I have an android app. The used cases are as below
From activity A can navigate to activity B via startActivity() and activity A is still on the 'STACK' (A is not finished).
Activity B is a subclass of google MapActivity
On Press of the BACK button , navigation should be from B to A.
The above use case works fine until I upgraded to Android 4.1.1(works fine from 2.3.3 to 4.0.x).
On my current 4.1.1(Samsung S3), the "BACK' button usually not working. When I press the BACK button, the button light up but no action is performed.
I even override the OnPressBack() method and placed a Toast message as a flag. But looks like the OnPressBack() is not triggered usually. Sometimes it works but most of the time it does not work(go to previous activity A).
And I also found the BACK button issue only happened with the subclass of 'MapActivity'.
Any clue will help. Thanks...
If you're using the new navigation components, you need to make sure the activity that contains your navController overrides the onSupportNavigateUp() function:
override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
Related
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
}
I have an app with MainActivity.java which has a button that when clicked launches Main2Activity.java. Main2Activity.java is added to the project in Android Studio using New/Activity/Basic Activity. It has a back arrow at the top and touching it will return the app to MainActivity. AndroidManifest.xml has the following for Main2Activity:
android:parentActivityName=".MainActivity"
The app also has a BroadcastReceiver which, after performing its work, launches Main2Activity.java. When started this way (when the app is in the background), the back arrow in Main2Activity does not go back to MainActivity but instead exits the app.
I would like the back function of Main2Activity to always go to MainActivity. According to Navigate up with a new back stack, there seems a way to do this, but the example is meant for an activity launched by another app.
How do I set the back destination for an activity launced by a BroadcastReceiver?
There are two ways of doing this:
Providing the Up Navigation as mentioned in the link quoted by you in the question. It should work even when you're navigating to the Activity through a broadcast.
Use startActivities from the BroadcastReceiver to start both MainActivity and Main2Activity. This allows you to create a backstack when starting an Activity and it's parent is not on the stack.
I prefer approach 2 simply because it works even when you press the hardware back, not just the back button on the Action Bar. For achieving that with approach 1, you'd also have to override onBackPressed.
I need to remove some screens from the navigation stack depending on what button is pressed.
I have a Home screen and next to it there are 3 more screens where the user enters some data(let's call them S1,S2, S3).
S1 is opened from home, S2 from S1, S3 from S2.
Each of these 3 screens have a back and a next button.
If the current screen is S2 and I press the back button I would like to remove it from the navigation stack, otherwhise when I go back to S1 and I press the back button it will go to S2, which is wrong, it should go to home.
I found out that calling Finish() would do the work, but it is not working if I press the next button instead of back. Neither the NoHistory = true is working in this case.
Is there something that can manage this or do you have any idea to solve it?
When your screen is S2, when press the back button you could calling Finish() to remove it from the stack, when press the next button you could use StartActivity(typeof(S3)) to open S3.
You have 2 solutions there :
1) You can override the OnBackPressed method on your activity. This will erase the classic behavior of the back button. So, for example in S2, you can override your OnBackPressed in order to navigate explicitly to S1. But be careful with that, because in this case you should also override the OnBackPressed method of S1 in order to avoid going back to S2 when the back button is pressed.
You can override OnBackPressed on your activity this way :
public override void OnBackPressed()
{
//navigate to the screen you want to
}
2) You can also set, in your activity, NoHistory = true. In your question, you said that the NoHistory = false statement does not work for you, which is perfectly normal since you have to put it true if you don't want the activity in your history stack.
Hope it helped !
I have an Activity that opens a new Activity for result like this:
Intent i = new Intent(ActDocument.this, ActCustomers.class);
startActivityForResult(i, ActDocument.DIALOG_CUSTOMER);
when I press back in the child Activity and return back to the parent Activity onActivityResult is called in all of devices correctly but I have fount a device that onCreate is called instead of onActivityResult.
This device is Samsung Galaxy Tab-P5100 Android 4.0.3. It is strange that I have tested my application on other devices of Samsung Galaxy Tab-P5100 Android 4.0.3 and it was OK but I have problem only in this device.
Why? How can I solve that?
Update
I noticed onDestroy() of parent Activity is called after opening child Activity on this device.
most of the time Back button will finish the activity unless you specific override the
OnBackPressed()
I think your problem is lack of resources, or maybe you have some developer options like kill activty flag or limit background process check this.
Update
Go to setting-> developer options unchecked don't keep activities and background process limit set to standard limit.
Perhaps the back button doesn't finish the Acivity. So, override onBackPressed.
I am programming an Android application and have a curious issue.
My application has a LoginActivity that defines the filter for launch events.
As soon as login is complete, it starts the "Home" activity using startActivity(new Intent(LoginActivity.this, HomeActivity.class)) and stops the LoginActivity using finish().
The HomeActivity is a simple dashboard with notifications, overriding onCreate and onStart. Also it updates the some content icons using an AsyncThread.
The problem is this: If I hit the Home-Button to exit my app and then use the "recent" menu (holdpress the Android-Home Button) to reopen it, the back-key is 'broken' in my app: Pressing it will not finish the HomeActivity, but instead loop back to the same activity:
Meaning ... HomeActivity <- HomeActivity <- HomeActivity <- HomeActivity ...
I have not used any hacks to override the backstack or back key behaviour.
Anyone got a clue what the cause of this may be?
TIA, Patrick
Maybe your login activity detects that login is complete and sends you immediately back to your home activity. That should be visible from the log (ActivityManager, START intent ...)
In that case it may be a good idea to play with the backstack