keep activity going in background when switched to another activity? - android

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

Related

How to bring the same activity but different task to the top of the stack?

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.

How to know if all fragments were detached?

On my app I am using a Main Activity, which has a Navigation Drawer, and as the user go to an options from the drawer, it will change the fragment beging displayed accordingly to the option selected.
If the user hit's "Back button" several times, it will go back to a point in which it will reach my Main Activity, which is a blank and empty layout.
When I reach this point (my main activit, empty), I would like to exit the app, or, open the Navigation Drawer.
The problem is, I don't know any event that shows me that I am back to the Main Activity. I checked the onResume, but it's never called, which makes sense, since the main activity has never been stopped.
I thought perhaps there would be an event from the fragment manager that would be called on the Main Activity when a fragment was detached, and from there I could check if there was no fragment at all attached?
When you push your first fragment, add a tag to it. Something like
transaction.replace( R.id.rootContainer, firstFragment, "rootFragment");
Whenever user presses back button, you can get your rootFragment from FragmentManager
FirstFragment myFragment = (FirstFragment) getSupportFragmentManager().findFragmentByTag("rootFragment");
And check if this fragment is visible or not, by myFragment.isVisible(), if not, then keep popping the stack, if it is visible, it means user is on the first fragment. Now you can either quit the app, or show your menu drawer.
Good night good sir. Thank you for your tip.
I used a different approach, based on your repply, that gave me quite a few insights.
On main activity, I Overrided the onKeyDown and check if it was pressed the Back Button. If yes, then I check the size of my Back Stack.
Uppon that, I decide if I want to finish my application.
Also, thanks for the tip of "labeling" the fragments on the back stack, didn't know I could do that.

Finished a set of activities, possibly in a task

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.

Specialize onOptionsItemSelected in a tabbed activity

I made a tab activity, menu logic is handled in the main activity because operations are the same.
I need to add a single line of code just for an activity for a specific option...is it possible to specify a general behaviour in the main activity and then, if needed , change something in sub activities?

Start activity in tab using fragment

I have tab activities (tab1, tab2).
When button1 is pressed in tab1 activity it covers up the whole screen. How to start another activity in the same tab?
Is it possible to start ContactsContract.Contacts.CONTENT_URI in tab1?
Check FragmentTabs.java example in the documentation.

Categories

Resources