Customize back navigation - android

First of all I read this article but is kind of complicated for me . All I need is to go to an activity I want (navigate if exist in back stack or start it with Intent if it's not ) in onBackPressed() override . should I manage back stack
or something else ? if yes how ? and if no what is a simple way for that ?
P.S : I dont use ActionBar

you should override the second activity's onBackPressed() method and add android:launchMode="singleInstance" to your first activity in AndroidManifest.xml this will launch the first activity from backstack or create new if it doesn't exist in backstack.

Use startActivityForResult() wherever needed. Do not create new instances of the same activity. That is, if u want to go back to the previous activity, just call finish() from this activity.
Understand different launchmodes
As mentioned in the link above, singleInstance launchMode might be tricky and might cause issues.

SOLVED: as mentioned in this answer and according to comments , if we want to just bring an (existing )Activity to front , setting Intent flag to :
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
wont create new instance of Activity we want and just bring it front . this would be useful if we have multiple activities on top of the one .

Related

Creating a back button to a previous activity (CONCEPT ONLY)

Before I say anything else, I am not asking how to make this work for my program, I want to figure it out myself. I have already seen one billion snippets of codes and answers. But I think I am not understanding the concept behind it and would like to verify if my understanding is correct.
Creating a back button and making it work:
1) Find and edit the xml for the action bar so show a back button.
2) Use the buttons id to create OnClickListener.
3) In OnClick, use intent to previous activity.
Is it not this simple? What are the proper steps conceptually for make this work?
That might work, but it wouldn't be the norm.
Depending on how you launch an Activity, simply calling finish() on an Activity will take you back to the previous one.
easier to
1) use AppCompatActivity
2) enable homeAsUp (back button)
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true)
Android activities are stored in the activity stack.
So simply by calling the finish() method if you started the activity with startActivity()
this.finish()
or if you opened the new activity from another activity with startActivityForResult()
call the finishActivity() method from your code on the click of the button
this.finishActivity()
And it'll take you back to the previous activity.
Well..... I don't know this answer would be helpful but, you said
3) In OnClick, use intent to previous activity.
However, how about thinking this situation?
If there is fragment and you press 'back button' in there, you should go before fragment not before activity. :)
You said you want to figure it out yourself so, I won't say more(about codes) as you wanted. Good Luck!

Is there a way to set an android activities attributes conditionally?

In my Android application I have an activity where I've been using [Activity(NoHistory=true)] to keep the activity from appearing on the activity stack.
Now I'd like to make it conditional - sometimes it should be on the activity stack so the user can press Back to return to it from a subsequent screen; sometimes it shouldn't. Is there a way to "decorate" an activity conditionally or must I write some code to accomplish this?
Thanks in advance.
Are you looking for intent flags that can be set like the one below ? You could use that to start an activity and set the flag conditionally.
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Activity Process

I have 4 activities. A,B,C and D.
From each activity user can start any activity.
i.e. Activity A has 3 buttons to start B,C and D. In same way all other activities also has 3 buttons.
Now my question is If user go in this sequence A->B->C->B.Now If user press back button then this sequence happens. B->C->->B->A. I want to change in this way B->->C->A. how can i do this?
I don't clearly understand your question, but maybe try use:
android:launchMode="singleInstance"
You want to keep the same instance all the time, not create a new one, yes?
finish activity B using finish() when you moved in the forward direction that means A>B(finish())>C>B
Like #Krishnakant said you need to add finish(); everytime, As far as I'm aware the back button is called as the Achille's heel for android programming as coding or adding handlers to it is not easy.
use intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); before staring any activity.

Android how to create activity only if it wasn't created yet

I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.

Android history only for 1 activity

I would like to know if it's possible to keep only 1 of each activity in history? I have tab-like interface and when user goes between activities it remembers them in history.
I tried noHistory attribute but it will cause activities to not go into history at all.
Ideally I'd like to have history but make it work like this:
Scenario:
Act1->Act2->Act1->Act2
In "standard" setup when clicking "back" it will unwind like
Act1->Act2->Act1->GONE
In "noHistory" mode it when click "back" it will be:
GONE
I want only 1 copy of each in stack, so it will be:
Act1->GONE
Possible?
When you call startActivity(), add FLAG_ACTIVITY_REORDER_TO_FRONT to the Intent. That will reuse your existing activity instance.
have a look at this:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
I think you may want
android:launchMode="singleTop"
in your activity's manifest if I understand your needs correctly

Categories

Resources