Android Studio - How to manage onBackPressed event? - android

Description:
I have an activity (Activity A) with a button that changes to another activity (Activity B) which has another 3 buttons. Button A goes to Fragment A, Button B goes to Fragment B and Button C goes to Fragment C. I am overriding onBackPressed method:
Question:
How can I manage onBackPressed method in order you canĀ“t go back to Activity A (which is a login activity) but can go back, for example, from fragment C to B?
Edit:
I am using the following script to go back between fragments:
getActivity().onBackPressed();

You can remove an activity from the history stack. You can achieve this by setting the android:noHistory attribute to "true" on Activity A.
<activity
android:name=".Activity_A"
android:noHistory="true" />
but if this is a particular case you can launch your activity A from other Activity D in this way
//From activity D
val intent = Intent(this, Activity_A.class)
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
startActivity(intent)
Other way to remove Activity_A from stack is calling finish() after launch your Activity B

Related

On back return to first activity and not its parent

I have an activity A that once the user presses a button it opens activity B.
I do that using:
startActivity(intent)
finish()
The user in Activity B has the option to click on an item and navigate to activity C or press the back button.
Problem:
When pressing the back button, I don't go to Activity A but to its parent.
How can I make sure that on back navigation I go to Activity A, while if the user clicks on an item in Activity B they end up in Activity C?
Suppose you have 4 Activities : A , B , C and D.
User goes from A -> B -> C , i.e. from Activity C user goes to B onBackPress and then A.
But if user goes A -> B -> C -> D, here onBackPress user goes to Activity A.
To implement this you can follow this approach
Start Activity B from Activity A - without calling finish()
Start Activity C from Activity B - without calling finish()
Here you backpress works fine as each Activity is in stack
Start Activity D from Activity C - without calling finish() /or call finish() doesn't matter in this case , as user never goes back to Activity C.
OnBackpress() of Activity D, override the method (onBackPress()), and start a new Activity A with clear the backstack (intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); )
Or for Activity A you make play with launch modes, Make Activity A SingleTask so when you again start Activity A from Activity D, same instance of Activity A will be called clearing all tasks(activites) at top
Just add
startActivity(intent)
Don't call Finish() While redirect to Activity B. It will resolve your back navigation problem.
override onBackPressed() method
#Override
public void onBackPressed() {
super.onBackPressed();
//Set your intent
Intent intent = new Intent(currentAct.class,Activity A);
startActivity(intent);
}

Android back button behaviour issue...

I have been using finish() for back button presses to go back to previous activity and its working fine but it is not working as expected for this specific scenario.
Here are a list of my activities and its functions:
Activity A - Show online forum topics
Activity B - Show the comments of a forum topic
Activity C - Post a new comment
After a user post a new comment, he will be directed to activity B.
Issue:
When i click on back button on Activity B, it will go back to Activity C, because it was my previous activity.
Activity A -> Activity B -> Activity C -> Activity B -> Activity C -> Activity B -> Activity A
Expectation
I want the user to go back to activity A from activity B at all cause.
Activity A -> Activity B -> Activity C -> Activity B -> Activity A
I tried using intent to only direct Activity B to A upon back pressed, but it is reloading the data on Activity A which i do not want.
Codes I tried:
case android.R.id.home:
finish();
return true;
//this works if i am only going back and forth one activity
case android.R.id.home:
Intent i = new Intent(ActivityB.this, ActivityA.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("forumLink", forumLink);
i.putExtra("forumTitle", forumTitle);
startActivity(i);
//this refreshes Activity A data
Call finish() on Activity C before going from C to B.
You can make Activity A as parent of B ,and B as parent of C
You have to kill Activity C before going to activity B.
Since your activity c exits, thats why it is going to activity c.
So, use finish() to kill activity c. Killing the activity c will redirect you to activity b.

Android back stack

I have an application with several Activities in Android A,B,C,D. I have a question about the back stack.
A->B->C->D it is a normal sequence.
My question is : when i press the back button in Activity D, i have to go to the Activity B, not back to ActivityC. Since ActivityC have some imageView i want to save, i don't want to use noHistory to destroy the ActivityC. it is possible to do that
A1->B1->C1->D1
the back stack should be: D1->B1->A1 . C1 saved some imageView and if C1 launch again, the imageView in C1 will contain the same images. Is it possible just modify the code in ActivityC??
thank you
You should call finish() in activity C after, starting of activity D
startActivity(intent);
finish()
Handle onBackPressed() of each activity. So that after back pressing on D, it will redirect to B, then similarly to A.
Use onActivityResult() methods of an activity:-
Here is how it will work -
Use startActivityForResult() method while you are starting activity
While when you are going back
Activity D - before finishing write -
Intent i = getIntent();
setResult(RESULT_OK);
finish();
Your D is finished and c's onActivityResult() will be called - save your data and call finish() with above methodology, It will go to B's onActivityResult().

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

android:parentActivityName vs onBackPressed startActivity

In Android application, does somebody know
what is the difference between :
override onBackPressed in an Activity and startActivity
vs
put "android:parentActivityName" in manifest in the activity tag
Thanks
android:parentActivityName :
The system reads this attribute to determine which activity should be
started when the use presses the Up button in the action bar. The
system can also use this information to synthesize a back stack of
activities with TaskStackBuilder.
This attribute was introduced in API Level 16.
means if you have three Activities A,B and C in your Appliction.you have set android:parentActivityName=".A" for Activity C in Manifast
when you start Activity B from Activity A and C from Activity B.then user Press back button from activity C.user automatic go to Activity A instead
of Activity B.
onBackPressed :
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
called when user press Back key from any Activity. onBackPressed finish Current Activity and resume Previus one.for example
if you start Activity B from Activity A and Activity C from Activity B. if user press back button from Activity C then
System finish Current Activity C and Resume B.

Categories

Resources