Reputation: 85
I have a form to be filled by user and next button "Next" in Activity1.
When user clicks "Next" button second activity Activity2 is started.
In Activity2 i have previous button "Previous".(* not device back button)
So when user clicks " Previous" button, Activity1 should be opened with the entered details in the form.
Activity1 should not be refreshed.
Seached alot on stackoverflow but no luck..!!!
Upvotes: 1
Views: 3471
Reputation: 122
I would refrain from using 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();
Upvotes: 0
Reputation: 1306
Why don't you use startActivityForResults
and then in the started activity finish()
In the started activity you have access to your intent in the onCreate method getIntent();
then you can use setResult(Activity.RESULT_OK, result);
or 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).
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();
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. 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
<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
<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>
Upvotes: 3
Reputation: 4954
Try overriding onBackPressed
method inside second activity and call it on click event of previous button
Inside Activity 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();
}
});
Upvotes: 0
Reputation: 2040
On previousButton onClickListener you can call finish(); This will close the current activity and reload the previous one from the stack. This is a quick hack.
Upvotes: 1