views:

561

answers:

2

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 it really dint help.So if you guys could help i would be very thankful.

Thanx n Regards,

HaKr

A: 

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 an better way to do that with services.

Hope this will help you!

Ungureanu Liviu
What i want is to get back to the parent activity that started this Not to some new activity
HaKr
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.
stormin986
A: 

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.

stormin986