In my app I implement a set of tabs to show activities instead of switching between fragments. All of these activities have a back button that I want to take the user out of the tabbed set of activities to where they were before they got to those screens. Is there a way I can easily finish all of the activities representing the tabs?
I looked into tasks but it seems like you can't just destroy a task and instead you have to start an activity in that task using CLEAR_TOP or CLEAR_TASK or something similar.
EDIT:
We use activities instead of fragments because each tab will be very large and complex/fully featured sections of the app that we thought would be better suited to an activity. The activities are started with REORDER_TO_FRONT. We do not want to use CLEAR_TOP since we would have to do a lot of requerying of data everytime we switched tabs. So for an example of the problem flow:
came from activity X and started activity A (the landing activity for the set of tabbed activities)
click a tab and switch to activity B stack looks like X->A->B
click third tab stack looks like X->A->B->C
when we click a "back" button on screen, all three activities should be removed from the stack so it becomes just X. But if we call finish it will make the stack look like X->A->B which we do not want.
Related
I have a simple application consisting of one MainActivity and two fragments, FragmentA and FragmentB. I want to use a NavigationGraph such that users still land on MainActivity but then can use two buttons to navigate to either FragmentA or FragmentB. Is there a way to do that? Based on the stuff on DAC, it looks like I have to use three fragments plus the MainActivity. But I was wondering if I can just use my existing scheme of 1 Activity and 2 Fragments.
Adding details to avoid confusion?
MainActivity has two buttons: User clicks on "Do A" to go to FragmentA and on "Do B" to go to FragmentB.
However, to use the Navigation Graph, it looks like I have to add one more fragment -- MainFragment. My question is: is there a way to avoid having to add an additional fragment and still use Navigation Graph.
Android applications need at least one activity to show UI. A fragment is just like a button in an android app. You cant show it without attaching it to an activity. In the android navigation tutorial, there's one activity. That activity only contains tag so it is actually empty. In fragment tag, you show a fragment and that tutorial show you how to switch fragment inside tag. So you are always inside MainActivity even when your app show a different UI(switching between fragment).
just like ianhanniballake comment above.. What does your activity show? What do you expect it to show? you always see it.
Edit:
Navigation graph only works for fragments. If you want to navigate between activities, use this Code:
Intent switchActivityIntent = new Intent(this, TargetActivity.class);
startActivity(switchActivityIntent);
You can use navigation graph like usual and when you need to open activity just use that code. It works fine.. But why do u need to use more than one activity when u using navigation graph? you should only have one activity when using navigation graph. That is why google make that thing(navigation graph) to support Single-Activity architecture.
I start activity A(1) and click button in Activity A then it start activity A(2) again but content in activity A(2) change. And I want to bring A(1) to top (swap with A(2) ) How do I do?
Example Flow
1) A(1)------>A(2)
when I click button in A(2), A(1) bring to top of stack.
2) When I backpress from A(1) that bring to top aleardy, it will show A(2).
Thank you and Sorry for my bad english.
You can't do this. There is no way to rearrange the activity stack reliably if you have multiple instances of the same Activity in the stack. Android wasn't designed for this kind of navigation.
If you really need this type of navigation in your app, you have 2 choices:
1) Don't use the same Activity twice. Even if the code and layout is identical you can create another Activity that has a different name. In this case, you will have A and B instead of A(1) and A(2). Now you can use Intent flags like FLAG_ACTIVITY_REORDER_TO_FRONT to rearrange the stack of activities in your task.
2) Don't start a new instance of A if you just want to change the content. Just change the content when the user presses the button. Override onBackPressed() and change the content again when the user presses the BACK button. This will give the illusion of 2 activities using only one.
I am implementing a dashboard plus action bar UI, like in the Twitter app:
Each button on the dashboard takes the user to a different activity. A few of these activities are more important than the others, and I could imagine the user switching between them via the dashboard reasonably often.
I feel like I have two options:
Keep an activity cycle going using intent flags, so that when the user goes back to the dashboard it just pushes the dashboard activity to the top of the stack. Then when the user returns to another activity, it pushes that one to the top of the stack. No activity would be destroyed until the OS does it to gain back memory, which would be fine.
Let the activities be destroyed when the user goes back to the dashboard, then recreated later.
Which option is better in terms of performance and best practices? I like option 1 but am not sure if I'm abusing the purpose of those intent flags. And if I do go with option 1, should I also override what the back button does so that finish() isn't called?
Personally I like the first option better. This way, you would easily remember the state of the other activities when the user returns to them.
For example, if in a child activity the user scrolls some list, then goes back to the dashboard, and then returns back to the child activity, the scroll position will be where he left it off.
Regarding memory, I don't think it's an issue. Let's take a tab component for example (which is a parallel navigation controller to your dashboard). With a tab control, all the child activities (the tab activities) are never destroyed as well.
If memory does become an issue, I would combine your two ideas. For the less important activities I would implement approach 2 (destroy them on back), and for the more important activities (where state is important for the user for example), I would implement approach 1.
I've searched SO and found several answers to the question in general, and have tried them all and am not having success. I really don't have my head around how the back stack works, Intent flags or the finish method. Here's my setup:
On application start-up, there's a splash screen where a couple AsyncTasks run in the background and check a couple webservers for updated content. ProgressDialogs report status. When complete (via the last onPostExecute), I launch a new Activity ("Home"). This seems to reflect some of the other posts, but I think my kludge is due to Home being a TabActivity, with 4 tabs, that initially calls setCurrentTab on tab 0.
So, using the suggestions previously posted:
android:noHistory="true" on the Splash activity
calling Splash.this.finish() after it launches the Home TabActivity
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_CLEAR_TOP
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_NO_HISTORY
The users sees the splash, the TabActivity launches, the user clicks to another tab, then hits back - the application closes (not force close - just closes back to the devices home screen).
If I don't use any of those, when the user hits back after changing to another tab, they go back to the Splash screen and are stuck (I could add a button or something to take them to the Home TabActivity but that's not optimal).
The desired result is that the user sees the Splash, gets taken to the Home TabActivity, clicks another tab, then hits back, he should be taken back to the initially set tab (tab 0).
Any insight is appreciated.
TYIA
The back stack is actually officially called the activity stack - every time you start an activity, that gets pushed onto the top of the stack (unless you set one of those flags you mentioned).
This means that unless each tab in your main app is a separate activity, then the default back key behaviour will be to leave your main app activity.
You can control this by taking over the back key or by overriding the tab switching behaviour to start different activities.
I have my main activity going and a button going to a different activity. When I go to the other activity my main activity stops. How would I keep the main activity going?
Thanks in advance.
How would I keep the main activity going?
You wouldn't.
Depending on what it is that you are doing, you might consider moving it to a Service.
Yes, you can keep your main Activity running. My way is use a "CHEAT".
Example your main Activity is A, in A we have button btnA. Click this btnA will open activity B.
You create a tab bar with 2 tab contains A and B. After initialize tab bar, call
tabHost.getTabWidget.setVisibility(View.GONE);
to hide this tab. So you only see main Activity visible.
When btnA is clicked, only need to switch tabHost to B
tabHost.setCurrentTab(1);//1 is index of Activity B
This question & answer is similar with your question. You can refer to it.
Activities and sub activities