In my android application, I have following requirement.
Activity A --> Activity B(Go to A Option) --> Activity C(Go To A Option,Go To B Option)
<
I assume that you don't want to use Intent because whenever you use Intent for moving to activity A pressing Back key will move to the previous activity (activity C). In this case I would suggest you to include FLAG_ACTIVITY_CLEAR_TOP
flag. It will destroy all the previous activity and let you to move to Activity A.
Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
Alternatively, you can try the FLAG_ACTIVITY_REORDER_TO_FRONT
flag instead, which will move to activity A without clearing any activity.
For more, check this.
You can do this as:
On back press option:
onBackPressed(){ @Override public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
//Your method intent
Intent intent = new Intent(A.this, C.class);
startActivity(intent);
}
Using a button:
button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(A.this, C.class);
startActivity(intent);
// finish(); // may use finish if do not want endless loop
}});
Why can't you use,
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Save your current instance
}
"...This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle)..."
I think that is what you wanted. With this you can use Intent
from Activity C. Yes then it goes to onCreate
method, BUT there you are checking whether there is any saved instance. If there are any saved instance you no need to load the rest of the onCreate
items. It will speed your application.
If you want to re-create your Activity then you can use FLAG_ACTIVITY_SINGLE_TOP
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
By doing this your Activity A will not be re-created rather you will have to override onNewIntent()
which will be called in your Activity A.
UPDATE:
This was using Intent
but as your requirement was without using Intent
, so in that case better approach would be using startActivityForResult
as already there are couple of answers above I am not elaborating it.
There are two ways you can achieve this: By Starting Intent with applying Intent Filter Clear top stack, but according to your question you are not interested in this method. Second method is by Starting activities by StartActivityForResult.
By using intent flags:
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Second Way, by Using startActivityForResult:
In Activity B:
Intent intent=new Intent(B.this, C.class);
intent.startActivityForResult(intent);
onActivityResult method:
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK ) {
String res = data.getExtras().getString("result");
if (res.equals("A")) {
String msg = "RESULT: " + res;
Toast.makeText(Login2.this, msg, Toast.LENGTH_SHORT).show();
finish();
}
}
}
In Activity C
Intent intent = new Intent();
intent.putString("result", "Hello, World");
setResult(RESULT_OK, intent);
you can passon the context of Activity B to Activity C and then in Activity C call
((Activity) bContext).finish();
that'll close Activity B and then you can call
((Activity)getContext()).finish();
that'll cause Activity C to close and go back to Activity A as Activity B is already closed.