Android - finishing activity from another activity

2020-07-18 08:45发布

is there any way, how to finish certain activity from a stack? I have service, which looks for updates, and when update is found, it opens update activity, where prompt for installation will appears. But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.

Thanks

标签: android
5条回答
We Are One
2楼-- · 2020-07-18 09:23

Try this please.

In first activity, define this before onCreate.

static TabsViewPagerFragmentActivity activityMain;

Then, in onCreate of first activity, write this.

activityMain = this;

Then, in second activity, write this when you want finish first one.

TabsViewPagerFragmentActivity.activityMain.finish();

This will destroy first activity and when you press back on second activity, you will go back to home directly.

查看更多
Explosion°爆炸
3楼-- · 2020-07-18 09:37

If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent)
  {
    super.onActivityResult(requestCode, resultCode, intent);
    finish();
  }
查看更多
戒情不戒烟
4楼-- · 2020-07-18 09:40

very helpful code

classname.static object.finish();
查看更多
何必那么认真
5楼-- · 2020-07-18 09:43

Use finish() method of the activity class to finish certain activity

this.finish(); // you can also use the application context  instead of this 

Hope this will help you.

查看更多
家丑人穷心不美
6楼-- · 2020-07-18 09:43

But after the installation appears I want to finish update activity, because it is not necessary to still be on a stack.

When your "update activity" calls startActivity() to bring up "the installation", have it immediately call finish() on itself.

查看更多
登录 后发表回答