Android- Going Back to Previous Activity with Different Intent Value

Android- Going back to Previous Activity with different Intent value

What you want is startActivityForResult(). When you go from C to D, instead of using startActivity() use instead startActivityForResult(). Then when you want to return from D to C you use setResult() which can include an Intent object with extras to pass back to C.

I don't recommend doing this in onBackPressed() if you don't have to because this will not be what the user expects. Instead, you should return with this data with an event such as a Button click.

So, in C you will do something like

 Intent i = new Intent(new Intent(C.this, D.class);
startActivityForResult(i, 0);

then in D when you are ready to return

 Intent i = new Intent();
i.putExtra(); // insert your extras here
setResult(0, i);

then when you return to C you will enter this method (taken from the Docs)

protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));

/*
can also get the extra sent back through data
using data.getStringExtra("someKey");
assuming the extra was a String
*/

}

Going to previous activity with Intent Extras

Use

startActivityForResult(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"),2);

instead of

startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));

And in onItemClickListener Do

Intent intent=new Intent();  
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();

And get the data in OnActivityForResult of your VehicleActivity

Android: Go back to previous activity

Android activities are stored in the activity stack. Going back to a previous activity could mean two things.

  1. 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.

  2. 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

As mentioned in the comments, if the activity is opened with startActivity() then one can close it with finish().
If you wish to use the Up button you can catch that in onOptionsSelected(MenuItem item) method with checking the item ID against android.R.id.home unlike R.id.home as mentioned in the comments.

How to retain value after going from one Actitivity to another and coming back to the previous actitivity?

You're doing things wrong. You shouldn't be returning to the AddReminder by launching an Intent. You return by finish(), going back to the previous activity. To return a value from the AddReminder activity, use startActivityForResult to start the MapsActivity, and set a result bundle before calling finish.

How to send intent data back to previous activity when back arrow icon is pressed?

you can just override the onBackPressed() method with the same function of your buttons.

@Override
public void onBackPressed() {
//put Intent to go back here
}

Navigating back to previous Activity

1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
Android Activity Stack

2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.

if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .

here is an example of saved instance:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.penguin_main);

if(savedInstanceState!=null){
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
}
}

back to previous activity with intent

You want to call startActivityForResult() for this. So in your first Activity something like

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(i, SOME_REQUEST_CODE); // you give SomeRequesSOME_REQUEST_CODE an int value

then in your onItemClick() you need to call setResult() and send back the Intent. This will call onActivityResult() in your first Activity

listPerasat.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
NamaPrst = ((TextView) view.findViewById(R.id.txtListNamaPrst)).getText().toString();
Intent i = new Intent();
i.putExtra("NAMA_PERASAT", NamaPrst);
setResult(RESULT_OK, i);
finish();
}
});
}

In onActivityResult() in your first Activity, don't call getIntent(). This will try to use the Intent that originally started your first Activity. Instead, use the Intent you passed back

@Override
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
edtText.setText(data.getStringExtra("NAMA_PERASAT"));
}

See this answer for another example



Related Topics



Leave a reply



Submit