我想在android应用程序上做一些简单的事情。 怎么可能回到以前的活动。

我需要什么代码回到以前的活动


当前回答

默认情况下,所有新的活动/意图都有back/previous行为,除非你已经在调用活动上编写了finish()。

其他回答

将此添加到onCLick()方法中,它将返回到之前的活动

完成();

或者你可以用这个。这对我来说非常有效

 @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  int id = item.getItemId();

      if ( id == android.R.id.home ) {
         finish();
         return true;
       }

  return super.onOptionsItemSelected(item);
  }

首先,你需要记住的是,如果你想回到前一个活动。然后当使用Intent转到另一个活动时,不要调用finish()方法。

在此之后,您有两种方法从当前活动返回到前一个活动:

简单地调用:

finish()

OR

super.onBackPressed();

Android活动存储在活动堆栈中。回到以前的活动可能意味着两件事。

You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity. Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP. You can use this to shuffle between the activities in your application. Haven't used them much though. Have a look at the flags here: http://developer.android.com/reference/android/content/Intent.html

正如注释中提到的,如果活动是用startActivity()打开的,那么可以用finish()关闭它。 如果你想使用向上按钮,你可以在onOptionsSelected(MenuItem item)方法中捕捉到,检查android.R.id.home的项目ID,而不是评论中提到的R.id.home。

可以显式调用onBackPressed是最简单的方法 参考返回到上一个活动的详细信息

默认情况下,所有新的活动/意图都有back/previous行为,除非你已经在调用活动上编写了finish()。