Start activity and don't finish activity - android

I have a splash activity and a MainActivity in which I open some activites:
Resume: SPLAH--> MAIN --> A or B or C and from A or B or C I open more activities
but when I start activity and push the back button from A or B or C the app is finished and I would like the MAIN would be active.
I start activities so:
Intent i = new Intent(this, Listado_mapas.class);
this.startActivity(i);
this.overridePendingTransition(R.anim.entrada_derecha, R.anim.salida_izquierda);
It doesn't work but nevertheless when I open with the same way from B to another activity it backs prefectly. ¿?
Thanks in advance
I don't understand this behaviour !!
why it's running ?

I reviewed my code and in the first Activity SPLASH I had FLAG_ACTIVITY_NO_HISTORY.

Related

How to reorder singleTask activity without creating new one?

I have 2 activity (Activity A and Activity B)
Activity A is singleTask and Activity B has standard launch mode.
When application started with Activity A and launch mode is singleTask, now from activity A start new activity B on button click.
So Stack is Activity A, B
Now wanted to reorder Activity A which is single task from Activity B but it's finishing Activity B first and then create new Activity A.
So stack at the momemnt is only Activity A.
What I've tried from Activity B.
val intent = Intent(this, ActivityA::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(intent)
Problem: Without finishing Activity B required to reorder Activity A with launch mode singleTask.
Appreciated in advance.
You cannot reorder an Activity with singleTask launch mode to the front of the stack. An Activitywith singleTask launch mode is by definition the root Activity in a task.
Why would you want to do this? If you've configured the Activity as singleTask, you have told Android that you want this Activity to be launched as the root of a task. Why would you want to reorder this Activity to the front of the task? If the Activity can exist in the task somewhere other than the root, you shouldn't define it as singleTask!

How to go back to Home Activity from a specific activity without overriding back button

I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D
When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit
I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.
However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )
According to the doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So A will be brought back and B and C will be cleared.
when you start your new activity finish the old activity for example if you want to start C from B :
Intent I = new Intent(B.this , C.class) ;
startActivity (I);
B.this.finish();

Android start main activity from home launcher

In my app I have a main activity that is started when the app is launched from the app drawer. In the app there is another activity B that can be launched from the main activity.
If I press home while in activity B to return to home, Activity B will be started instead of the main activity if I launch my app through the app drawer again.
Is there a way to make only the main activity start even if the user pressed home in activity B without using noHistory?
Any help would be appreciated!
You just don't want to keep state in B, on the Activity B onStop method, just call finish();
I set a separate android:taskAffinity for activity B

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.

How to Handle android background activity

I have activity stack in my android application.Suppose A and B.When the application starts the Activity A is visible.From this activity i call Activity B using startActivityForResult method.Now from here if i press Home button of device then activity runs in background that is OK.But when i relaunch the application by taping on activity icon a new instance of activity is start and show activity A rather showing the activity B.Please help me in this.Any help would be highly appreciated.
Check out if your launchMode of the Activity A is SingleTask.
You can try calling activity B using getApplication().startActivity(myIntent);

Categories

Resources