我想在android应用程序上做一些简单的事情。 怎么可能回到以前的活动。
我需要什么代码回到以前的活动
我想在android应用程序上做一些简单的事情。 怎么可能回到以前的活动。
我需要什么代码回到以前的活动
当前回答
有一些情况可以回到你之前的活动:
情况1:如果你想把结果带回你之前的活动,那么 ActivityA.java
Intent intent = new Intent(ActivityA.this, FBHelperActivity.class);
startActivityForResult(intent,2);
FBHelperActivity.java
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
案例2:ActivityA——> FBHelperActivity---->ActivityA
ActivityA.java
Intent intent = new Intent(ActivityA.this, FBHelperActivity.class);
startActivity(intent);
FBHelperActivity.java
after getting of result call finish();
By this way your second activity will finish and because
you did not call finish() in your first activity then
automatic first activity is in back ground, will visible.
其他回答
您是否想要控制后退按钮的行为?您可以通过以下两种方法之一重写后退按钮(以转到特定的活动)。
Android 1.6及以下版本:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
或者如果你只支持android2.0或更高版本:
@Override
public void onBackPressed() {
// do something on back.
return;
}
详情请访问:http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
试着活动#完成()。这或多或少是后退按钮默认的功能。
除了上面提到的答案,还有另一种方法,假设你有两个类,A类和B类。
类A你做了一些活动,比如选择复选框,打印出一些数据和意图到类B。 类B,你想传递多个值给类A,并保持类A之前的状态,你可以使用,尝试这种替代方法或下载源代码来演示这一点
http://whats-online.info/science-and-tutorials/125/Android-maintain-the-previous-state-of-activity-on-intent/
or
http://developer.android.com/reference/android/content/Intent.html
默认情况下,所有新的活动/意图都有back/previous行为,除非你已经在调用活动上编写了finish()。
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。