Proper navigation between activities using navigation drawer and back button? - android

I have the following situation:
One Main ListActivity. The list consists of POJO objects, each item is a different POJO.
Each POJO contains different groups. For each group I made different Activity.
Lets say POJO1 contains groups A, B and C, when pressed, ActivityA will be opened and the navigation between the rest activities ActivityB and ActivityC is done using NavigationDrawer.
All group activities implement a custom class MyNavigationDrawerClass which is the core of the Navigation Drawer in my app.
Each ActivityA, B, C, etc, has a SettingsActivity which is strictly individual and depends on from where it has been started.
Now the question is: How to navigate from any A, B, C, etc, activity to the Main ListActivity every time the BackButton is pressed?
I will provide an example:
App is started - Main List Activity is showed;
Item is pressed and ActivityA with NavigationDrawer is started;
The NavigationDrawer is opened and ActivityC is opened;
When BackButton is pressed, the user is navigated back to ActivityA, but my desire is to navigate the user to the Main List Activity
What I tried:
To put android:noHistory="true" for every ActivityA, ActivityB and ActivityC in the AndroidManifest. That did not work because when I open the SettingsActivity for example from ActivityA and return, the user in navigated to the Main List Activity, and not to the ActivityA - it's confusing and misleading.
To override onBackPressed in MyNavigationDrawerClass and start the Main List Activity using Intent. That did not work either because there are still previous activities in the stack and when the BackButton is pressed from Main List Activity, the user is navigated to them.
I hope I explained the situation clearly.
Any help will be appreciated.

You can override the public void onBackPressed(){...} for redirect the user where you want like this :
public void onBackPressed()
{
Intent intent = new Intent(this.getApplicationContext(), MainListActivity.class);
startActivity(intent);
}
look at this link can help you

Related

Launching an Activity within a certain fragment, then going back to the previous Activity from where it was launched?

I've been struggling with a particular challenge which is as follows:
My app has an activity "A" which is considered the app's "main" activity. At a certain point, it launches an activity "B" which has an action available that should launch a fragment inside activity "A" (this won't always be the same fragment, it will depend on some data coming from our backend).
I can do this just fine by simply calling startActivity with the correct Intent, however, on pressing the back button, it goes back to A's "main fragment" (this is logic implemented inside of A's onBackButtonPressed()). Essentially, what should happen is as follows:
Activity A -> Activity B -> Activity A showing Fragment X -> press back -> Activity B
What happens when using startActivity to launch Activity A:
Activity A -> Activity B -> Activity A showing Fragment X -> press back -> Activity A showing the "main fragment". From here, if I press back again the app exits, which again is part of the implementation of A's onBackButtonPressed, however I've tried retrieving an extra from the intent which invoked A in order to conditionally bring back activity B but the Intent seemed to be empty of extras for reasons I can't figure out. I am sure I am correctly putting the extras in the Intent since activity A launches the correct fragment when invoked from B based on what I put there.
More things I've tried:
Launching the desired fragment directly from within B, however this way the fragment is not shown with the navigation bar that exists in A and seems to show the main contents of activity B behind the fragment's elements, which in user experience terms is undesirable.
Using the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag, which seemed to make no difference whatsoever.
As this is part of my company's app which already has a decent degree of complexity, I'm not at liberty to provide you with useful code samples, and hopefully my description is sufficient for someone to aid me.
Additional information
targetApi="n"
Testing on Android 11
Activity A has launchMode "singleTask"
Activity B has launchMode "singleTop"
For the intended behavior:
Avoid using any launchMode, taskAffinity or activity flags, the default behavior is absolutely good for your requirements ("standard" is the default launch mode). So when you do the action in Activity B, a new instance of Activity A will be launched after putting B in backstack, which is the default behavior.
You should have a logic in Activity A's onBackPressed() such that: if the fragment X is visible, then it will exit the whole activity, otherwise it passes by calling super(). Something like the following:
In Activity A
#Override
public void onBackPressed() {
Fragment fragment = getFragmentManager().findFragmentByTag("yourTagForFragmentX");
if (fragment instanceof XFragment) {
// The fragment is available in the fragment manager
finish()
} else {
super.onBackPressed();
}
}
For more details, here
I think you're overcomplicating things.
Remove all of the launch mode flags for A and B - they should not be necessary.
Remove custom handling of onBackPressed - default handling should suffice.
Update A to initialize itself to the correct fragment based on the intent it's given:
For example:
onCreate(...) {
if (getIntent().getAction() == "START_ON_X") {
// Notice we REPLACE and DO NOT add to back stack
getFragmentManager().replace(fragID, createXFragment()).commit()
}
else {
getFragmentManager().replace(fragID, createDefaultFragment()).commit()
}
}
Thus, you will have:
Default launcher intent launches A.
A launches B.
B launches A with specific intent to show Fragment X
This will give you a stack of A -> B -> Ax
Then when you press back, DEFAULT BEHAVIOR will leave you on B.
Then pressing back again will leave you on A.
Then pressing back again will close your app.
Again this is all standard, default behavior. KISS.

Preload Activity in backstack

When user click on a notification, I set up a backstack of Activities A -> B, where B is on top and shown to user. I would like the lifecycles of Activity A to run, so that when user presses back button and comes to Activity A, it is already ready. What could I do to achieve this?
This cannot be possible as android will not allow us to do that, the only way I can see you can achieve this by doing your Activity A operation inside your Activity B And provide those details to Activity A when the user pressed back button.
Note: If you are showing any kind of list on Activity A or doing similar kind of work, you can have one Singleton Class where you can get your data in Activity B which required by Activity A, and provide same data to Activity A when the user pressed back button.
You should open the activity as normal but then put this line of code in the OnCreate() method to bring to the back.
moveTaskToBack(true);

Activity launchmode and lifecycle

I have two activities A & B. in A, i am showing a list of titles, and on clicking a title, it will open the detailed article in activity B.
I declare A as singleinstance in Manifest.
But if I declare A as single instance, and when the Activity B is opened and paused, then Activity A is not available on backstack.
I will try to explain by reproducing:
Activity A (launchMode = SingleInstance) with list of titles.
On clicking a title, Activity B opens
On clicking back button/up navigation, Acitiviy B finishes and Activity A resumes.
Again open activity B.
Press home button of device (Activity B goes to background - onPause)
Activity B is available in Recent Apps
Open app from recent apps/launcer - Activity B opens
Clicking back button/up navigation - Act B finishes, but Act A not resumed.
How can I provide better up navigation?
For my idea, you can change lauchmode of Activity A and Activity B from Singleinstance to android:launchMode="singleTop". I work fine for me. It is well said here. Let try.
When you will press the home button the activity B would call onStop() method. For a better understanding you can refer to https://developer.android.com/guide/components/tasks-and-back-stack.html

Android How to go back to Activity B, which contains an ID from A, from Activity C?

I have three activities. Activity A provides a list of groups. When you click on a group you proceed to Activity B, passing the Group ID of the clicked item, so that you could see details of that group. When you press an "Add" button on Activity B, you will proceed to Activity C.
Using the back and/or up button of the action bar, I could navigate from Activity B to Activity A without any problems. I could reload again the list items of Activity A. But when I press the back button of the action bar of Activity C to go to Activity B an error occurs because the passed group ID from Activity A is not recovered.
Please tell me what is the best thing to do. I'm a newbie.
PS: I understand that there is the hardware back button but according to the requirements a back button from the action bar is required.
You do not need to kill activities when you navigate from Activity A to Activity B and then to Activity C. Most probably, you are killing the activities and this is the reason that reference to that groupId from Activity A is no longer available.
I think, you are intenting from Activity C to B. By doing this, you have called the onCreate method of Activity B. So i will suggest you to code list this in your Activity C
#Override
public void onBackPressed() {
finish();
}
When you press the back button in the action bar, are you creating the new intent and push the activity. if so then you have to pass the id to class.
if you call finish() instead of creating the intent in the onClick listener of the back button, I think there will be no issue in recovering the id.
In your manifest file use meta-data tag like this, e.g,
<activity ...>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.org.abc.board" />
</activity>

Killing Activities using Back Button

I'd like to kill all activities when the user hits the back button and returns to my main activity A. My App has the following 3 activities:
A -> Displays users selections before they hit search and creates activity B.
B -> Displays a list of urls's based on the users selection and creates activity C.
C -> Opens a Webview and displays the page selected by the user.
Currently, i don't call finish() after starting activity B so when the user hits the back button in activity C they can return to the list of url's and make another selection if they want.
I'd like to create a new activity A (without the users initial selections) and ensure the existing activities B and C are killed if the user hits the back button in activity B?
Any help would be appreciated.
Thanks
O.
You can return to activity A by overriding the back button handler in activity C and launching activity A with the flag FLAG_ACTIVITY_CLEAR_TOP set, or alternatively (perhaps preferably in your case), you can just set noHistory for your activity B.
You'll have to reset activity A manually in some way in this case though.

Categories

Resources