Synchronous system activities - android

I wanna run two system activities one after another in specific order.
now as we know, startActivity is an asynchronous operation, so i cant keep on a specific order.
so i thought maybe I should try to do it with dialogBox in the middle but also running a dialogBox is an asynchronous.
now as i said the activities which i try to run are system activities, so i cant even start them with startActivityForResult (or mybe i can, but i cant think how it will help me).
Any tricks how could i manage with this issue?
Some code:
first activity:
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intent);
second activity:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(tmpPackageFile
.getAbsoluteFile()),
"application/vnd.android.package-archive");
startActivity(intent);
as you can see, i dont have any access to those activites, i can just run thire intents from an outside class/activity/service.

You should be able to use startActivityForResult.. The second parameter to that function is a unique id, which you can use to track which activity is ending.
In onActivityResult of the calling activity, check which activity just finished, then start the next one with another call to startActivityForResult (or, if you don't care what happens with the 2nd, just startActivity).

I may be missing the boat on this, it seems you should place your code to start the second activity in the handler that finishes the first activity, such as on a button press or when an item is selected from a ListView. More information on how the first Activity is terminated would help.

Related

Finish app from any activity [duplicate]

This question already has answers here:
Finish all activities at a time
(21 answers)
Closed 3 years ago.
I want to kill my app from a exit button on my navigation drawer. the drawer has a exit button that is suppose to just kill the whole app (all activity) and gets the user out of the app .
When i am using finish() in the first activity its working but when i go into the app and call finish() the activity gets killed and returns to previous activity but never really kills the app.
Tried to use System.exit(0) but no luck.
Extra Information Might Help
I have all my activity started with android:launchMode="singleTop" . it means all those activities that are already created will not be created again and just reordered to front on calling.
Do anyone have any suggestion for this , please do help.
Update
I want to make some updates here as my question looks like this SO Question.
As i have already said i am using a android:launchMode="singleTop" . and this answer is not working for my case. I have to call onCreate() to make this happen but it is not my case.
Use below code
public void AppExit()
{
this.finish();
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
Call above function to exit from App.
First, you need to decide if you just need to take the user out of the app, or if you also need to finish() all of your activities. It depends on your needs, but my guess is that in most cases most users won't know the difference, and Android is designed to have many apps having activities still running. If this is all you need, then you can just use an Intent to take the user to the home screen.
Intent intent = new Intent(Intent.
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
If you actually want to finish() your activities, then you need a mechanism to inform all currently running activities that the user is exiting the app. You can use something like EventBus to send an event that will be received by all registered subscribers; if you don't want to use a third-party library, you can do the same type of thing with LocalBroadcastManager. To do that, you would register a BroadcastReceiver with an IntentFilter for a custom action and broadcast an Intent with that action when the user wants to exit.
In either case, every activity should receive the signal and call finish() on itself. Note that the subscription (in the EventBus case) or the registration of a receiver (in the LocalBroadcastManager case) must be still active after onStop(), so I would register in onCreate() and unregister in onDestroy().
Use below code in your exit button
Intent intent = new Intent(this, ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And, in your ActivityOne.class oncreate() method just put below code
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
As a few answers say, call finish(); after you start an intent from an activity. That will make sure the current activity is the only running activity. Also try super.finish(), which will call finish() at parent activity.
You can alternatively use only one activity with many fragments. That way, if you call finish() in the exit button OnClickListener code, you'll exit the app. This will also save a lot of coding since you will be defining the navigation drawer once.
First of all, Android apps are not intended to be killed other that by the system, which attempts to keep them in memory in case the user will return.
But, if you have to do it, try this:
android.os.Process.killProcess(android.os.Process.myPid());

Android - start multiple activities

is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity

Close allactivities in Android app

I have an app for sending sms. It consists of several "cascading" activities - one for writing text, next for choosing number and next for confirming before sending. After sending the message I want that all activities get closed. How can i do this?
You will find no well-written Android apps that have this behavior.
After sending the message, you are welcome to send them back to your main activity using Intent.FLAG_ACTIVITY_CLEAR_TOP, which will remove all intervening activities between the current one and the main activity, using something like:
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If you start all the Activities with startActivityForResult you can chain the finish calls in onActivityResult: basically make the final Activity call finish after setting a specific result code, and each earlier activity checks for that result code and if its there does the same thing. They'll all close up cleanly and you should be all set.

Android New Intent New Screen

I am still not completely sure of this opening a new screen with a new intent. I have two problems. 1 is getting it to work and the second is more theory.
Firstly I have two packages com.quiz.max and com.reason.max both have activities names accordingly eg Quiz and Reason respectively. Here is the on click code I am trying to execute at the moment in quiz to go to reason.
Intent intent = new Intent();
intent.setClassName("com.reason.max", "com.reason.max.Reason");
this.startActivityForResult(intent, requestCode);
Secondly I heard if I start this intent then everytime i click the button a new intent is created. Does this mean if the user goes to reason page and navigates back and clicks the button again they actually create a new intent instead of going back to the already active one. Thus dozens could be opened via this method. Therefore should I close each reason intent once navigated back or is this a redundant point?
Max
I think you want
Intent intent = new Intent(this, Reason.class);
startActivityForResult(intent, requestCode);
Secondly, you don't "start an intent". You use an intent to ask an Activity to start, in this case the Reason activity. And yes, the default behavior is to start a new instance of the activity each time it is requested.
You can alter this behavior with the launchMode.
Make sure you read and understand the Activity lifecycle. You don't need to worry about too many Activities in existence, Android will handle that for you, but you should properly save state and clean up connections in the appropriate lifecycle methods.

How i can close/exit button on my app?

i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);

Categories

Resources