문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top