简体   繁体   中英

Android: activity will be recreated after calling finish

I'm trying to create an app which contains two activities: MainActivity and SubActivity .

SubActivity will be created from MainActivity by calling startActivityForResult() .

When I tap the button settled in SubActivity , it should finish itself and go to MainActivity . But I get problem with finishing SubActivity . After SubActivity.onPause() is called, SubActivity.onCreate() will be called immedietly and the SubActivity will be recreated.

I just wanna close SubActivity and display MainActivity , but why is this happen?

I searched for same questions and found that rotation change will make the system call onCreate(). But in my case, the screen orientation fixed to portrait.

Could someone please give me an idea to solve this problem?

EDIT: I solved the problem. There was a wrong logic in the MainActivity .

SubActivity will be created from MainActivity , when a variable holds a specific value. The display update method which reflects the value of the variable was called twice in code. That brought my App to recreate the SubActvity after finish() .

MainActivity requests to create Subactivity twice, but since my app is set as android:launchMode="singleTop" , the second SubActivity could be only created after the first one has been finished. Thanks a lot to give me advice!

You can use or first check the behaviour of,

1) finishAffinity();

2) finishActivityFromChild();

May it work.

Otherwise for better communication put some line of code here so we will understand proper and give some guidance to you.

In my case, I called below functions.

btn.setOnClickListener {
    setResult(result)
    finishActivity(REQUEST_CODE)
    finish()
}

As you describe I am giving you example. From MainActivity call the SubActivity using startActivityForResult() method

For example:

Intent intent = new Intent(this, SubActivity.class);
startActivityForResult(intent, 1);

Now in your SubActivity set the data which you want to send back to MainActivity . (Below is both example)

With data example:

Intent intent = new Intent();
intent.putExtra("key",result);
setResult(Activity.RESULT_OK, returnIntent);
finish();

No data example: (If you don't have data send to MainActivity )

Intent intent = new Intent();
setResult(Activity.RESULT_CANCELED, intent);
finish();

In your MainActivity Override onActivityResult() method to retrieve result. (Only if you send data from SubActivity )

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String data=data.getStringExtra("key");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM