[英]In android, How to resume previous activity , on button click from second activity
I have a form to be filled by user and next button "Next" in Activity1. 我有一个要由用户和Activity1中的下一个按钮“下一步”填写的表单。
When user clicks "Next" button second activity Activity2 is started. 当用户单击“下一步”按钮时,将启动第二个活动Activity2。
In Activity2 i have previous button "Previous".(* not device back button) 在Activity2中,我具有上一个按钮“上一个”。(*不是设备后退按钮)
So when user clicks " Previous" button, Activity1 should be opened with the entered details in the form. 因此,当用户单击“上一步”按钮时,应使用表格中输入的详细信息打开Activity1。
Activity1 should not be refreshed. Activity1不应刷新。
Seached alot on stackoverflow but no luck..!!! 在stackoverflow上有很多问题,但是没有运气.. !!!
Why don't you use startActivityForResults
and then in the started activity finish()
为什么不使用startActivityForResults
然后在启动的活动中使用finish()
In the started activity you have access to your intent in the onCreate method getIntent();
在开始的活动中,您可以通过onCreate方法getIntent();
访问您的意图getIntent();
then you can use setResult(Activity.RESULT_OK, result);
然后可以使用setResult(Activity.RESULT_OK, result);
or setResult(Activity.RESULT_OK);
或setResult(Activity.RESULT_OK);
(For canceled Activity.RESULT_CANCELED
) to return result code and data and after you set the result you call finish and then return (code doesn't exit the methods if I remember correctly). (对于取消的Activity.RESULT_CANCELED
) 返回结果代码和数据,并在设置结果后调用finish然后返回(如果我没有记错的话,代码不会退出方法)。
Then in the first activity you get the result and handle what to do with it in: 然后,在第一个活动中,您将获得结果并处理结果:
onActivityResult(int requestCode, int resultCode, Intent data)
Another option : You could also use the logic for back button pressed calling the method from your code: super.onBackPressed();
另一个选择 :您还可以使用后退按钮逻辑,以从代码中调用该方法: super.onBackPressed();
Edit 编辑
As I promised here's an Example 正如我所承诺的,这是一个示例
Two activities - first one have two TextView
s and a button next that launches the second activity -> in the second activity two EditText
s in which you enter some data which is then returned to the first activity when you press previous button. 两个活动-第一个活动具有两个TextView
和一个按钮,第二个活动将启动第二个活动->在第二个活动中,是两个EditText
,您在其中输入一些数据,然后在按上一个按钮时返回到第一个活动。 If you press back button instead of previous button, you enter in the canceled logic which is not doing anything in the example there is only a comment. 如果按后退按钮而不是上一个按钮,则输入取消的逻辑,该逻辑在示例中不做任何事情,只有注释。
MainActivity 主要活动
TextView textViewFirstName;
TextView textViewFamilyName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewFirstName = (TextView)findViewById(R.id.first_name_edit_text);
textViewFamilyName = (TextView)findViewById(R.id.family_name_edit_text);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//requestCode here is 12345 that we passed when we started SecondActivity
if(resultCode == Activity.RESULT_OK){
String resultFirstName = data.getStringExtra("firstName");
String resultFamilyName = data.getStringExtra("familyName");
textViewFirstName.setText(resultFirstName);
textViewFamilyName.setText(resultFamilyName);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Canceled logic
}
}
public void nextButtonClick(View view) {
Intent i = new Intent(view.getContext(), SecondActivity.class);
//If you want you could pass some additional data, like which action to take
//if you're reusing the second activity for more than one use case
i.putExtra("someAdditionalData", "Some string that you want to pass");
//12345 is int that you pass and will be returned as requestCode in onActivityResult
startActivityForResult(i, 12345);
}
SecondActivity 第二活动
EditText editTextFirstName;
EditText editTextFamilyName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editTextFirstName = (EditText)findViewById(R.id.first_name_edit_text);
editTextFamilyName = (EditText)findViewById(R.id.family_name_edit_text);
Bundle bundle = getIntent().getExtras();
String someAdditionalData = bundle.getString("someAdditionalData");
}
public void previousButtonClick(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra("firstName", editTextFirstName.getText().toString());
returnIntent.putExtra("familyName", editTextFamilyName.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
}
activity_main.xml activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:text="Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:hint="First name"
android:id="@+id/first_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:hint="Family name"
android:id="@+id/family_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:onClick="nextButtonClick"
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
second_activity.xml second_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity">
<TextView
android:text="Second"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:hint="First name"
android:id="@+id/first_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Family name"
android:id="@+id/family_name_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:onClick="previousButtonClick"
android:text="Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
On previousButton onClickListener you can call finish(); 在previousButton onClickListener上,您可以调用finish();。 This will close the current activity and reload the previous one from the stack. 这将关闭当前活动并从堆栈中重新加载前一个活动。 This is a quick hack. 这是一个快速的技巧。
Try overriding onBackPressed
method inside second activity and call it on click event of previous button 尝试覆盖第二个活动中的onBackPressed
方法,并在上一个按钮的click事件上调用它
Inside Activity 2 内部活动2
@Override
public void onBackPressed()
{
super.onBackPressed();
}
And call this on previous button click event 并在上一个按钮单击事件上调用它
buttonPrevious.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
I would refrain from using finish(); 我将避免使用finish(); cuz it kills the activity from where it's called. 因为它杀死了被调用的活动。 Try to use: 尝试使用:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
instead of finish(); 而不是finish();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.