The Scenario :
I have four activities : A, B, C and HomeActivity. A is my launcher activity. Am using actionbarSherlock, so A,B and C have menu option in the bar.
The flow is :
A-> B-> C --**On submit in C**--> HomeActivity
Now when i press Back button on Home Activity, it goes back to activity B as after clicking Submit in C , am using
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //Shouldn't this clear A,B and C ??
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
finish();
But i would like to remain on HomeActivity only as data is submitted and then it was started.
Also if menu button is pressed on A,B,C, then HomeActivity is started and in that case, i would like to have default behaviour of Back button(i.e go back to activity in which menu was pressed)
Any insights on how to do this as FLAG_ACTIVITY_CLEAR_TOP not serving the purpose!
(P.S. : HomeActivity is not a launcher activity)
I think you finish your Home Acitivity while you go to the Home Activity -> Activity A. When you are using clear top flag then your Home Activity should be alive in your stack. Please make sure that you are not finish your Home activity.
And also put this code onKeyDown() method in your Home Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("onkeyDown>>>>", "Called>>>>>");
finish();
}
return super.onKeyDown(keyCode, event);
}
check more details check this link:
Other Way
EDIT:
try to put above code into Activity A.
remove finish() method from your code like below:
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //Shouldn't this clear A,B and C ??
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
And Most Important thing Don't Finish your Activity A launcher
Activity for Clear top method it should be in stack of Activity remain
present in your device.
Hope it will solve your problem.
Related
I start in main activity A. From A I go to activity B and from B I go to C. Now in activity C if a button is clicked (not back button) ,activity B and C need to close and a new instance of activity B needs to be created. But if that particular button is not pressed and back button is pressed then activity C is closed and we return to previous activity B. I am making a reminder app. Activity B is a recycler view showcasing all the tasks and Activity c is to create new task. So if on activity C submit button is pressed I want activity C and B to be closed and a new instance of b to be opened i.e. recycler view with new tasks but if submit is not pressed I want to close activity C and return to original instance of B. How to do so?
Maybe you can use this 2 methods
(1) Use IntentReceiver
From second activity
Intent i = new Intent(SecondActivity.this, ThirdActivity.class);
i.putExtra("SecondActivity", new ResultReceiver(null) {
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
SecondActivity.this.finish();
}
});
startActivityForResult(i, 1);
In third activity
((ResultReceiver) Objects.requireNonNull(getIntent().getParcelableExtra("MainActivity"))).send(1, new Bundle());
//And after this finish current activity (Second activty)
startActivity(new Intent(ThirdActivity.this, FirstActivity.class));
finish();
(2) If Clear all previous and current activity
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
You can use the method finishAffinity() to exit an activity. I hope this at least makes the problem a bit easier.
Start Activity B with the intent flag FLAG_ACTIVITY_CLEAR_TOP
https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP
In my project i move from main activity to activity A and from activity A to activity B. From activity B using home menu on toolbar i jump back to main activity. Now when i press back button application should exit but it opens the activity A again.
You should make use of the launch flags to manage your activities in the back stack. As far as I understood your scenario, I think you need to use FLAG_ACTIVITY_CLEAR_TOP for starting your main/home activity.
Read more about launch flags: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP.
Also, take a look this for more details on managing activity back stack on Android: https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks
Call the finish() method before starting the next activity to have it removed from the activity. Find more details and options here.
For Kotlin write this in your MainActivity :
override fun onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
For Java write this in your MainActivity :
#Override
void onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
Hope to it will work for you as good as for me
hey i write some code for you
Variables:
boolean backactivity = true;
CODE:
public boolean onOptionsItemSelected(MenuItem item){
if(backactivity==true)
{
finishActivity(1);
backactivity=false;
}else
{
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return true;
}
I have 3 activities: A,B,C. When I click on an item in RecyclerView in activity A activity B will be opened. There are a few String values which I have passed from A to B.
In activity B when I click on a button I go to activity C. In C when I press back button from my phone(android default back button) it comes back normally to activity B. But I have also put a back button in ToolBar using parent activity. When I press that button it shows me error that the String value trying to retrieve in activity B is null. I understand why the error is showing. It's obviously because there is no string passed from C to B to fetch.
But why am I not getting this error when I press back from my phones default button? How can I solve this?
Activity A
Intent intent = new Intent(getActivity(), Chat.class);
intent.putExtra("Recievers_Id", userId);
intent.putExtra("Recievers_Name", userName);
startActivity(intent);
Activity B
MessageSenderId = mAuth.getCurrentUser().getUid();
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
MessageRecieverName = getIntent().getStringExtra("Recievers_Name");
Activity C
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Getting an error on
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
MessageRecieverName = getIntent().getStringExtra("Recievers_Name");
because obviously nothing to fetch... how do i solve this
Add onClickListener to the back button of your toolbar:
toolbar.setNavigationOnCLickListener(new View.OnClickListener){
#Override
public void onClick(View view) {
finish();
}
});
If you are getting error for null then try this:
if(getIntent().getStringExtra("Recievers_Id") != null){
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
}
if(getIntent().getStringExtra("Recievers_Name") != null){
MessageRecieverName = getIntent().getStringExtra("Recievers_Name");
}
On activity B you should check is Intent has rquired Extra.
Intent i = getIntent();
if (i.hasExtra("Recievers_Id")) {
messageRecieverId = i.getStringExtra("Recievers_Id");
}
if (i.hasExtra("Recievers_Name")) {
messageRecieverName= i.getStringExtra("Recievers_Name");
}
You can set the working of back button on toolbar as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
First of all, you need to understand what happens when user presses navigation up button and what happens when onBackPressed.
Based on the documentation:
If the parent activity has launch mode <singleTop>, or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought
to the top of the stack, and receives the intent through its
onNewIntent() method.
If the parent activity has launch mode , and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP, the parent activity
is popped off the stack, and a new instance of that activity is
created on top of the stack to receive the intent.
Where as when back button is pressed, it navigate, in reverse chronological order, through the history of screens. In this case Activity won't be re-created.
To summarize, the lauch mode of your ActivityB in AndroidManifest.xml must be standard. In this case the activity will be recreated when you press the navigation up button. But when back button is pressed, ActivityB is just brought to front, hence the difference in behavior.
Solution:
Approach 1: handle Up button by yourself
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== android.R.id.home {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Approach 2:
Set launch mode of Activity B to singleTop in AndroidManifest.xml. Remember to check the implication of changing the launch mode.
The "standard" and "singleTop" modes differ from each other in just
one respect: Every time there's a new intent for a "standard"
activity, a new instance of the class is created to respond to that
intent. Each instance handles a single intent. Similarly, a new
instance of a "singleTop" activity may also be created to handle a new
intent. However, if the target task already has an existing instance
of the activity at the top of its stack, that instance will receive
the new intent (in an onNewIntent() call); a new instance is not
created.
I have 2 activity and when i go to A from B and then i press "back" it creates antoher activity but i don't return to the same
When you go activity A to activity B. Just use startActivity(yourIntent) without finish(). Like-
Intent goNext = new Intent(A.this, B.class);
startActivity(goNext );
Then when you go back from activity B to activity A, just use finish() method on your activity B. Like -
#Override
public void onBackPressed(){
finish();
}
OR
You can use startActivityForResult()
Let's say I'm in activity A. I want to start activity B, but I want the user to be taken to activity C if they press back on activity B.
So even though the user sees A -> B, I want it to be A -> C -> B.
I know I could use TaskStackBuilder and synthetically create the ABC stack. However, sometimes A isn't simply one activity; there might be some previous navigation the user did that I don't want to lose and that would be too much trouble to synthetically recreate with TaskStackBuilder.
Is there a way to use TaskStackBuilder keeping the current back stack? Or is there any other way to synthetically add an activity to the back stack before starting another one?
What you may do is override onBackPressed() in Activity B, from where you will launch activity C and finish activity B. From user's perspective he will see:
A -> B -> (back press event) -> C
In backstack:
A -> AB -> AC
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
finish();
}