If i have the following
Parent activity > activity 1 > activity 2 > activity 3
Pressing the back button will go back to:
Parent activity > activity 1 > activity 2
Pressing again will go to:
Parent activity > activity 1
is there any way that I can programatically finish activity 1,2 and 3 from activity 3 itself.
In IOS there is a function called popToRootViewControllerAnimated, Which is the type of concept I require in Android
thanks
Launch the child Activities with startActivityForResult() and then in onActivityResult() call finish(). This will bring you back to the Parent Activity.
Alternatively you can also use the Intent flag FLAG_ACTIVITY_CLEAR_TOP while calling startActivity() on Parent Activity from Activity 3.
Add Flag Intent.FLAG_ACTIVITY_CLEAR_TOP while you call startActivity from your last activity. It will clear the activity stack
Check in onResume() of activity2 if you came from activity3 and call finish(), if yes. Same for activity1.
maybe this algorithm can be usefull;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if (keyCode == KeyEvent.KEYCODE_BACK) {
//here you'll check if activity3 alive then join
//else if activity2 alive then join
//else if activity1 alive then join
//else finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
Related
I have a fragment which opens an activity (let's call it activity1). This activity (activity1) can open another activity (let's call it activity2). And again this activity (activity2) can open another activity (let's call it activity3). I would like to get the following behavior:
When pressing back in activity1 it will go back to the fragment - works.
When pressing back in activity2 it will go to activity1 - works.
When pressing back in activity3 it will go back to the fragment - does not work.
The last one does not work because it goes back to activity1. When moving from activity2 to activity3 I finish activity2. But how can I also finish activity1? I can't use the (Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK) flag like it was suggested in previous topics because I want it to go back to already opened fragment. How can I remove activity1 from the intent stack?
if you want the flexibility to have activity3 to show any activity when it finishes, you need to override onKeyDown and handle KEYCODE_BACK.
in Activity3.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//show fragment or startActivity to show activity with fragment
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I am starting B Fragment from Fragment A.Now from Fragment B i hit Home Button.Again i open the app and it calls OnResume. Now if i hit back button it exit from the app.What should i do?
Fragment A to Fragment B Activity
Intent find = new Intent(getActivity(),FindActivityMain.class);
find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
***startActivity(find);***Here i am not finishing the activity.
Fragment B Activity to Fragment B transaction code
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).commitAllowingStateLoss();
}
Fragment A Activity is "SingleInstance" declared in Manifest.In General the Back Navigation works Perfect.But If i do these step ->
1.) Click Home Button
2.) Again long press home button to restore the app
3.) Click back button.Exit from the app.
This 3rd Step should not occur.
Please Help !!
remove find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
add your code 'addToBackStack(null)'
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).addToBackStack(null).commitAllowingStateLoss();
}
Inside Activity B add this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
I have 3 screens . 1->Fragment(inside fragmentactivity) 2->Fragment 3-> Activity. In screen 3 I call screen 1 to do something and I return again to screen 3. Now if I press back button in screen 3 I want to go to screen 2 instead of screen 1 . I tried this logic but it returned back to screen 1 instead of 2 .Any suggestions please?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d("TAG","back button ");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
if you want remove activity form Activity Stack you need call finish() method or start activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, as second solution remove all activity from Stack you don't need that.
in your situation you need call finish() method on following step:
1-> 3
3-> 1
use addtobackstack in your fragment transaction:
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
http://developer.android.com/training/implementing-navigation/temporal.html
I have a main activity. It's main purpose is to handle Tab menu. If a user click a tab it'll load another activity as a sub-activity, still showing the tab menu. I am using Intent to do this:
setContent(new Intent(this,B.class))
This sub-activity has an onclick function as well. If the user clicks one of the links it'll load xml layout file using setContentView command:
setContentView(R.layout.B1);
Now, when the back button is pressed while xml file is loaded, it'll close the entire application. Is there a way to prevent this, say, return to the sub-activity or the main activity?
thanks for all your help.
You should override the onBackPressed method in your activity or sub activity:
#Override
public void onBackPressed() {
//TODO Do your task here
}
In your sub activity you should override the fallowing:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) //Override Keyback to do nothing in this case.
{
return true;
}
return super.onKeyDown(keyCode, event); //-->All others key will work as usual
}
I want to send back my activity to starting one. Let me explain you.
I am using Main activity having 4 tabs, and each tabs has his own activity. Each activity in has more activities in it.
Problem starts when I click on 1st activity in my 1st tab. The next activity is independent. I create an intent of my next activity. In such case to prevent draw my tab-bar layout in that new activity. I use my existing activity layout, remove all the view in it. Then i put new activity in it.
e.g..
contentViewLayout.removeAllViews();
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
Its done. But problem arise when device BACK button pressed. Because new activity is starting in existing activity. So when back button pressed , then it quite to main activity and application close. I want to do that in such a case "new activity" should be terminate and existing activity must start again. And then if again BACK button pressed then it should terminate main activity.
I override the onKeyDown() down method as follow but it. dose not working properly?
In my new activity.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
childActivity = null;
contentViewLayout.removeAllViews();
mainActivity = new Intent(this,MainActivity.class);
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
return false;
}else{
return super.onKeyDown(keyCode, event);
}
}
But it always quite the application instead of closing child activity
I got it!
Actually the focused activity was the main activity... not the new activity. So all we have to do to set focused to our new activity. So in onCreate() method of new activity I put focused to one any component in new activit e.g..
button.setFocusable(true);
button.requestFocus();
So in such a case new activity onKeyDownButtonWorks
Blockquote
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ){
//My code goes here.
return true;
}
return super.onKeyDown(keyCode, event);
}
Take a look at this solution:
ActivityGroup not handling back key for ListActivity