How should I manage the activity stack? - android

I have an app with a main menu at bottom; I can't figure out how should I manage the activity stack, because every button opens an activity, and each activity can start more activities, and i was looking for a management in the style of the current Instagram's app. It looks like (in the Instagram app) that every activity started by each button in the bottom menu opens a new activity stack, but when you press back button, it navigates in the reverse order you called every activity.
Sorry for my bad explanation, i hope that you can understand my aim.
You could check out Instagram Android app to figure out what is my goal.
My current implementation uses a MainActivity with a Fragment for the first menu button (Qui in giro->"Nearby"), but i probably should change this approach.
Thanks.

1) Firstly you will have to decide what will be your main activity.
2) Now use this main activity like as star topology. Means this main activity will be center activity.
3) Use to finish() method for finish back stack before receiving at this center activity.
4) After it if you open another side of your main activity than you will see only new back stack will be displayed after pressing back button for receiving at main activity again.

Related

How to configure the back stack when activity is launched by BroadcastReceiver?

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.

how did facebook messenger switch between activities

how can facebook hide current activity and show another activity here in chathead
when choose another one to talk with him its hide current popup floating activity and show the other one activity
i don't think its start new activity because i write some message on current activity in the editText without send it and when choose another one to talk with him its hide current activity and when i back open it the text i write it it's still there thats mean it's dosen't start new activity when switch to other conversation
iam try to make something like facebook chathead but i have problem i switch between floating activities
They are using Fragments and Floating Action Buttons.
Also just for your information, unless you specifically call finish() on an activity, it will only get stopped when the new activity is started. When you return, it will go back to onResume() state.
Please look into activity life cycle and Fragment Life cycle to know about these things more clearly. The faster you have a clear idea about these, the easier it will be in the long run
https://developer.android.com/training/basics/activity-lifecycle/index.html
https://developer.android.com/guide/components/fragments.html
NOTE: When it is on the home screen, It is an window running on top of the background service with a floating window , while inside the app it is a Fragment.
see tutorial: http://androidsrc.net/facebook-chat-like-floating-chat-heads/

How to finish a specific activity in android

I know you might think that this question have been asked a lot, but I've been looking into different cases, all cases they just want to close all activities when logged out. Here is my scenario:
1) Start the app with Splash Screen then I used finish()
//I set this activity as MAIN so it's first to open - that is why I cannot setFlags(FLAG_ACTIVITY_CLEAR_TOP) because it basically goes back to MAIN
2) Introduction Pager(4 sliding pages) with Login and Signup buttons
3) Login and signup buttons direct you to corresponding activities
// I didn't use finish in introduction pages because I want the user to have the ability to go back to introduction if for example pressed signup by mistake
4) after performing login/signup the user will be directed to the homepage(Here I used finish to kill login/signup activities because I won't need them anymore)
So all previous activities are now finished except for the introduction pager, when the user press back the app will finish homepage and go back to introduction, but I don't want that! I just want the app to quit, I don't want the introduction pager to be running on the background. How do I finish introduction pager when onCreate method is called in homepage?
I have another related question I didn't want to post another thread for it: how to get the previous activity? like I don't want to use the action bar to navigate, I created a button (<- Back) and when I press it I want to bring the previous activity, how do I get the previous activity?
First of all make your IntroductionActivity a single istance in the manifest like this: launchMode = "singleInstance"
There are many ways you can do this, the fastest, I guess, is to use a static field that refers to that activity. Add this in your IntroductionActivity:
public static Activity mActivity;
onCreate(){
this.mActivity = this;
}
Then when you reach the homepage you can do this:
onCreate(){
IntroductionActivity.mActivity.finish();
}
Use finish(); after you start the next activity
Example:
Intent itr=new Intent("com.example.splash.Second");
startActivity(itr);
finish();

android - How to Exit from the application

My Application have 20 activities. Here i want to implement the how to exit from the application when you click on the button(like Logout). it means if you click on the menu button any where of our application then it shows the one button. if click on that then directly comes out from the application. how to implement it.
thanks
Well naresh you can do something like that
first finish the activity from which you are closing application this.finish(); secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this clear top when you switch from one activity to another activity and as you know every activity is kept in stack to so this flag remove old activity from top and push new activity in top so around your whole application only one activity is kept in stack
and if this does not work put the whole application in background by avtivityname.moveTaskToBack(); this will move your whole app in back ground but only one drawback when you start your activity it will show your that activity from which you have moved back
System.exit(0);
should work, don't forget Java common functions work on android, there isn't only the android library!
As for the button being in the menu in every activity, you could create a class derived from Activity which creates and handles the menu properly, and make every other activity inherit that derived activity.
First finish the activity from which you are closing application: this.finish();. Secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); This clears top when you switch from one activity to another activity. As you know every activity is kept in a stack to so this flag removes old activity from the top and pushes new activity to the top so around your whole application only one activity is kept in the stack.
If this does not work, put the whole application in background with avtivityname.moveTaskToBack();. This will move your whole app to the background. One drawback: when you start your activity it will show your that activity from which you have moved back.

android, starting and exiting activities

I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.

Categories

Resources