Sending Activity to background without finishing

前端 未结 5 976
臣服心动
臣服心动 2020-12-05 14:53

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

5条回答
  •  情深已故
    2020-12-05 15:34

    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.

提交回复
热议问题