I'm working on an app that uses tab groups to switch between activities. I know that the TabActivty is depreciated and using views instead of activities is the better way to go, however due to time constraints I don't have the time to rewrite the app.
So here is what is going on. I have 4 tabs, lets say Tab1, Tab2, Tab3, Tab4.
Let's say Tab1 has ActivityA > ActivityB > ActivityC and I am on ActivityC. When I switch to Tab2 and hit the back button, I would like it to switch back to Tab1.
I'm trying to accomplish this by overriding the onKeyDown method however when I do this, it takes me back to Tab1, then closes the current activity. So where I was on ActivityC, when it switches back to Tab1, ActivityC closes and leaves ActivityB. This is a problem, especially if I am on ActivityA because it will close the app out. Here is the override method I'm using. Any help is appreciated!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
#SuppressWarnings("deprecation")
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTabByTag("tab1");
return false;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed(){
//Do Nothing
}
If you return true instead of false, it should not be handled by anybody else, which should prevent the Activity from closing. Does that help?
Activity.onKeyDown()
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 using Fragment A,Fragment B .. and so on. i want to navigate back to the previous Fragment, from the current Fragment.
I want to use Back Button in Mobile to get back navigation.
How can i do this ? OR i have to make Back button in layout xml.
i tried below code,
fTransaction.add(R.id.container, nF).addToBackStack(null).commit();
when i pressed Back Button in Mobile , it restarts the App.
To go back to previous fragment using phone's back button you should override onKeyDown() mehtod.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
getFragmentManager().popBackStack();
return true;
}
return false;
}
When replacing a fragment use replace instead of add and then addToBackStack:
fTransaction.replace(R.id.container, nF).addToBackStack(null).commit();
Try this:
fTransaction.addToBackStack(null).add(R.id.container, nF).commit();
I want to go back to the last fragment in the back stack, so I want to make the back button popback the stack. Should I do this? and if so, should I override onBackPressed() or onKeyDown()?
#Override
public void onBackPressed()
{
Intent intent = new Intent(this,ABC.class);
startActivity(intent);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
My fragments aren't being added to the back stack properly for some reason
I am using this to try to go back to the previous fragment, but the order is acting strange. What exactly should I do to make the order proper?
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
if(teamsFrag !=null)
{
if(manage.getBackStackEntryCount() > 0)
manage.popBackStack(manage.getBackStackEntryAt(manage.getBackStackEntryCount()-1).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
//ft.detach(dataFrag);
}
Short answer? No.
Long answer? If you have to ask, No.
You should set your fragments up using the fragment manager so that the back button does what you want. You shouldn't override the back button instead of implementing your stack correctly.
I want to go back to the last fragment in the back stack, so I want to make the back button popback the stack.
This is the default behavior when you use FragmentTransaction#addToBackStack() while adding new Fragments with the FragmentManager.
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