开发者

Closing a Activity on new Activity

开发者 https://www.devze.com 2023-02-28 17:28 出处:网络
I have an activity that has a button with a intent as follows Intent i = new Intent(getApplicationContext(), MyMainActivity.class);

I have an activity that has a button with a intent as follows

Intent i = new Intent(getApplicationContext(), MyMainActivity.class);
                   startActivity(i);
                   finish();

Once you move to the next activity MyMainActivity.class if you hit the back button it shows the last activity with the old resaults.

Is there a way t开发者_运维技巧o make the activity close or move back once the button is pushed? This way once the activity is done it is not my activity history.


use this along with intent

Intent i = new Intent(getApplicationContext(), MyMainActivity.class);
 i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   startActivity(i);
                   finish();


Intent i = new Intent(getApplicationContext(), MyMainActivity.class);
startActivity(i);

best practice is to @override the onPause() in the activity. onPause() method is called whenever the control moves to new activity.

so call the finish() method in the onPause().

@Override
protected void onPause() {
    super.onPause();
        finish();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消