Sending Activity to background without finishing
Asked Answered
I

5

19

How can I make an activity go to background without calling its finish() method and return to the Parent activity that started this? I tried so much but I could not find a solution. So if you guys could help I would be very thankful.

Ineffectual answered 11/1, 2010 at 12:53 Comment(2)
What is it that you're trying to achieve by sending the Activity to the background?Drunk
actually i'm having data that needs to be cached.The parent activity has it dynamically loaded first time and then it sends it to its child activities .A child activity modifies the data and that modified data should be there .but if it finishes ,previous data is sent to the child activityIneffectual
S
50

The following will work if the Activity is running in a different task to its parent. To achieve this, see http://developer.android.com/guide/topics/manifest/activity-element.html#lmode.

public void onBackPressed () {
    moveTaskToBack (true);
}

The current task will be hidden, but its state will remain untouched, and when it is reactivated it will appear just as it was when you left it.

Speedboat answered 15/8, 2011 at 21:57 Comment(4)
There is absolutely no guarantee that the state of the Activity will be untouched. In all likelyhood, the activity will be destroyed by the OS. Unless you are responding to the lifecycle events and saving/restoring the state accordingly, your Activity will be agnostic to the fact that it was created and moved backwards.Vargas
Jonathan is correct that there is no guarantee that background activities will be kept running. But in normal circumstances they will not be destroyed by the OS until some time has passed. If destruction would cause a problem for you, then you must save and restore state as other commenters have said.Speedboat
So onSaveInstanceState() will ensure my state can be recovered afterwards, right?Kerchief
You should add @Override to the method in order to avoid bugs and expected behaviors.Philippi
M
2

If you have data you want to cache / store / process in the background, you can use an AsyncTask or a Thread.

When you are ready to cache / transition to parent, you would do something like the following in one of your child Activity methods (assuming you started child with startActivityForResult() )

Thread Approach:

Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            // PUT YOUR CACHING CODE HERE

        }
});

t1.start();

setResult( whatever );
finish();

You can also use a Handler if you need to communicate anything back from your new thread.

AsyncTask Approach:

new CacheDataTask().execute( data params );
set_result( whatever );
finish();

The AsyncTask is for situations in which you need to process something in a new thread, but be able to communicate back with the UI process.

Md answered 28/4, 2010 at 3:2 Comment(0)
C
2

Simply

protected void minimizeApp() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}
Chasse answered 27/7, 2017 at 9:49 Comment(1)
Thank you, you save my life :)Carbo
T
0

Try:

 Intent toNextActivity = new Intent(CurrentActivity.this,
                                     NextActivity.class);
 CurrentActivity.startActivity(toNextActivity);

If you use this way, the method onPause() from CurrentActivity will be called and if you have a static variable (like a MediaPlayer object) in CurrentActivity it will continue to exist (or play if it is playing)..

I'm using that in my application but I found a better way to do that with services.

Hope this will help you!

Thaxton answered 11/1, 2010 at 13:8 Comment(2)
What i want is to get back to the parent activity that started this Not to some new activityIneffectual
Using an Intent to transition to a new activity will only run the normal lifecycle activities such as onPause() and onStop(). What won't happen is a process continuing to execute while a new Activity begins to load and execute. See my answer for solution.Md
V
0

Moving the task backward is NOT going to insure that you will maintain any state, unless you do that manually by responding to the Activity lifecycle events.

You should just start the second Activity via Intent using startActivity(), or, if you need to receive a result back from the second Activity, use startActivityForResult(). This allows you to receive a callback when the user finishes the Activity and returns to your first Activity.

Starting an Activity and getting results: http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Vargas answered 19/2, 2012 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.