Back Button works not as Expected with TabGroupActivity and ViewSwitcher - android

I have some strange effects in my Android App.
I use a TabHoster with TabGroupActivities for each Tab. Works pretty good except the behaviour of the Back Button.
An Activity is launched and responds to the Back button. Then I start a Child Activity with a ViewSwitcher. When i hit the Back Button on View A Activity is dismissed as Expected. But it gets through the onBackPressed() of my Activity.
When i hit the Back Button on View B (detailView==true) the Method is not even Called. Instead of that the onBackPressed() of the TabGroupActivity is Called and I am not able to switch back to the first View.
#Override
public void onBackPressed() {
if (detailView == true){
vf.showPrevious();
detailView = false;
}
else {
super.onBackPressed();
}
return;
}
Can Anybody explain this and/or tell me how to switch between Views in an Activity in an ActivityGroup?

Few Days later i came to the Conclusion that it is FOR MY CASE the easiest solution to throw away the ViewSwitching stuff and transfer them to seperate Activities.
Sebastian Olsson is surely right with the fragments but it would be more effort for my specific App to rebuild everything to fragments.

Related

Android phone built-in back button

I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}

Pressing back on last fragment shows blank Activity shortly before going to previous Activity

I have two Activities and each one has 3-4 Fragments that each replace the other one.
When Activity1,Fragment2 launches the second activity, Activity2,Fragment1 is launched and onBackPressed closes the Activity2 and goes back to Activity1 as normally.
When I'm launching Activity2 from Activity1,Fragment3 though, I want to add Fragment2 to the 2nd activity, which is done via cone in the onCreate method.
However, when the back button is pressed, the fragment disappears for a split second, leaving Activity2 blank on the screen and after that Activity2 is closed, resulting in a visual glitch.
Is there anything I can do to close Activity2 the exact moment (at least at the same visual moment) as when Fragment2 is being closed by the back button?
Thank you.
P.S.: Not adding the Fragment to the backStack seems to close the activity immediately, which is what happens in the first case (expected behavior), but when adding the Fragment to the backStack (as needed for correct operation in the second case) produces the glitch.
Ok, after fighting with the backStack for many days, I decided to skip it, since it does not seem to be working properly, or I'm not understanding it.
The way I did it is on my Activity's onBackPressed i'm checking which fragment is showing and if it's the one that should be the last one (Multiple ones are the "last one") I just finish() the Activity.
#Override
public void onBackPressed() {
MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag("MyFragment");
if (myFragment != null && myFragment .isVisible()) {
//if myFragment is visible when the back button is pressed...
finish();
} else {
super.onBackPressed();
}
}
Don't forget to give a name to your fragment in the FragmentManager transaciton. In my case it is "MyFragment".

How come sometimes when I press back button, there are more Activities than there should be?

I don't know how to phrase this question.
My app has a simple ViewPager and you click on something in one of those page Fragments and it takes you to another Activity. Press the back button and it takes you back to the ViewPager. Press back again and it closes the app.
However, sometimes when I press the back key at the ViewPager, it takes me back to the other Activity again, and then I press back and it takes me to the ViewPager, and so on, until the app finally closes. In other words it's like there are spare, leftover instances of either the ViewPager or the other Activity in the backstack (I think it's called), but I have no idea how or why this is happening.
I also don't know how to reproduce the error, which is even more frustrating. Most of the time it works as expected, but sometimes the leftover instances just show up out of nowhere as I am pressing back.
How can I guarantee better that when I press the back button on the other Activity, it goes back to the ViewPager, and when I press back on the ViewPager, it closes the app?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: //called when I press back button on the phone or the back-arrow on the toolbar
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//and then usually that's all I have, but in one particular Activity, I do override the function:
#Override
public void onBackPressed() {
if (some conditions are met) {
finish();
super.onBackPressed();
}
else {
//display some error message
}
}
Here is how I launch my Activities:
public void launchSomeActivity() {
Intent intent = new Intent(getActivity(), ActivityName.class);
intent.putExtra( etc etc etc );
startActivityForResult(intent, REQUEST_CODE);
}
Likely you are seeing a nasty long-standing Android bug. If you launch your app for the first time from an IDE, or from the installer, when you put the app in the background and launch it again from the HOME screen, Android creates another instance of your root Activity. This would exhibit exactly the behaviour you are reporting. If you launch the app for the first time by pressing the app icon from the HOME screen (or list of available apps), the problem does not occur.
See Re-launch of Activity on Home button, but...only the first time

Android UP button goes to mainActivity instead of current

I have 3 activities:
MainActivity (start activity with grid view)
FragmentActivity (full screen image slider accessed from grid view)
InfoActivity (blank activity opened from menu in either Main or Fragment)
When I go from MainActivity to InfoActivity:
startActivity(new Intent(MainActivity.this, InfoActivity.class));
and press the "up" button I get back to main activity.
When I go from FragmentActivity to InfoActivity
startActivity(new Intent(this, InfoActivity.class));
and press the "up" I STILL get back to MainActivity.
I know it's because my MainActivity is the parent of Info.
But how do I make the "up" behave like the "back" button, so that I can go from InfoActivity to FragmentActivity?
Any help will be much appreciated!
You have to override onOptionsItemSelected in the InfoActivity class to intercept the "up" button and call onBackPressed from there. Like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null && item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
The answer from #Firoze Rakib will work but I would like to provide you better understanding of the problem and solution advised by Google.
First of all you most probably have defined parent in your AndroidManifest.xml, you should remove it since your InfoActivity can have different parents.
Secondly, there is a small difference between BACK BUTTON(which is programatically called in #Firoze answer) and UP BUTTON. First one simply destroy current activity and show activity that was previously shown, theoretically it does not have to be any of your parents. The UP BUTTON starts new instance of the parent Activity and clear the activity stack in the current task.
If this difference matters for you then you should follow instructions from Android Developers page. Take a look at second point that starts with this sentence:
Or, override getSupportParentActivityIntent() and
onCreateSupportNavigateUpTaskStack() in your activity.
how do i make the "up" behave like the "back" button...
I believe another answer has the technical details if you are determined to do this (which you should not).
The purpose of my answer is to point out that making up behave like back will confuse users by violating the Android design guidelines.
Up navigation is an alternative to using the back key with a different purpose: it gives the user a way to navigate back to the top of an app when they are "lost" deep in a hierarchy and they might have to press the back key many times to escape. In this situation some users will panic and start mashing the back key, and up nav is meant to give them an alternative.
Try this
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null )
if( && item.getItemId() == android.R.id.home) {
// your code
return true;
}
return super.onOptionsItemSelected(item);
}

Why Back button is not detecting in muti tab activities?

I have Main Activity. That has 4 tabs(TabHost). I have overridden onBackPress() in MainActvity , as well As All 4 activities. This button show user a dialog box and for conformation of Exit
When app start. It show 1st tab. Then if I press back it work fine. But if I go for next 3 tab and then press back, The app stop. OnDestroy() of Main is called. But there is not dialogue for the user.Even noting is print in log cat. That I have written in onBackPressed() method From and of 5 activities including MainActivity.
I have also try onKeyDown() for back key but result is same? Have any one experience the same? Please help me.
I come to know it was difficult to open new activity inside the previous tabs when I am using TabHost. I google it and found GroupActivity is the best solution for this problem.
GroupActvity Example
But GroupActivity have the same problem when open new activity in the previous tab. the back button not work properly for new activity. After search I found that was due to the focus was alwasys on parent activity. I have to make
setFocusable(true);
requestFocus();
on my new activity component to gain focus.
I am now using GroupActivity for customizing Tabbar and activities.As I ma also maintaining stack of activity ids in parent activity so that I can pop out the recent activity when user press back button.
else if you are NOT going to implement Activity focus then you should maintain stack in parent and when press the back button it will initiate parent onBackPressed(); and you can call the child onBackPressed() functionality as discussed in the link.
onBackPressed() not working inside ActivityGroup
well one thing i know for sure is that you will always get the onBackPressed() called in the MainActity if you are running with a tabHost and not in the child views. The only thing that comes to mind is if you have consumed the event in the onBackPressed method (return true) because if you didnt it will go and still follow the default process and destroy your activity.
I meet this problem,but i have shot it now.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showExitDialog();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void showExitDialog()
{
new AlertDialog.Builder(this)
.setTitle("Attention")
.setMessage("Do you want to exit this application")
.setPositiveButton("YES", exitListener)
.setNegativeButton("No", cancelListener)
.show();
}
at the first time i lost a "reture true" in onkeydown()

Categories

Resources