0

I have 2 activities: B and C.

B starts C, and then by user action, C finishes and I have A again.

Here is the code that takes the user from B to C

    public void goToC(View v) {

        Intent intent = new Intent(this, C.class);
        intent.putExtra("STUFF", stuff);
        startActivityForResult(intent, 1);

}

Here is the code in C that passes data back to B

long rowsUpdated = myModel.updateStuff(this, stuff);

if (rowsUpdated == 1) {
    Intent intent = new Intent();
    // put the data in the intent
    intent.putExtra("STUFF", stuff);
    // set the flags to reuse B
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // set result code to notify the ItemEntriesActivity
    setResult(1, intent);
    // finish the current activity i.e. C
    finish();
}

Here is the code in B that receives the above data

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent != null) {
            switch (resultCode) {
            case 1:
                // Update the class variable, so that onResume() gets the updated value
                stuff = intent.getExtras().getParcelable("STUFF");
                break;
            default:
                break;
            }

        }
    }

My problem is that this code works correctly, but whenever I go from B to C, the onStop() on B is called. I don't have any code that calls finish() on B. This forces the B.onCreate() to be called everytime, and makes my app slow. I also stried putting android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" in both the activities in my Manifest, but no luck. I have spent 2 days on this, and near the end of my wits. Any help is appreciated.

rgamber
  • 5,749
  • 10
  • 55
  • 99
  • Is `C` semi or completely transparent? I mean, after starting `C` is some part of `B` still visible? – nKn Feb 09 '14 at 22:51
  • No. None of the activities or semi or transparent. When I start C, C is wholly in the front, so no, B is not visible after starting C. – rgamber Feb 09 '14 at 22:57
  • Then James Black's answer is right. If C was semi-transparent, `onPause()` would be fired instead. – nKn Feb 09 '14 at 23:04

2 Answers2

1

If you read this page

http://developer.android.com/training/basics/activity-lifecycle/stopping.html

It explains that when your activity starts another the first will stop.

If you want to keep the first running have the business logic in a Service.

Here is a tutorial on that

http://www.vogella.com/tutorials/AndroidServices/article.html

Or from the second activity return enough information to continue where it stopped, perhaps some information before it is stopped.

James Black
  • 41,583
  • 10
  • 86
  • 166
  • I see. Thanks for the answer. I think I will modify the code to hit the database in the **onCreate()** of **B**. Seems like that will involve less overhead than keeping a service running in the background.! – rgamber Feb 09 '14 at 23:08
  • just out of curiosity, in this situation the `startActivityForResult()` and `Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP` are useless then, right? – rgamber Feb 09 '14 at 23:13
  • I wonder if this will help you.http://stackoverflow.com/questions/17506432/flag-activity-clear-top-destroys-target-activity-and-than-creating-it – James Black Feb 10 '14 at 00:20
0

To bypass onCreate() instead of startActivityForResult you can just start activity C from B (do not call finish() on B). Then in C start activity B with flags Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP. Put the info you want to pass back to B in the intent bundle. Then C will be finish and B will be brought to front but with onNewIntent being called instead of onCreate.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54