I have an activity called FooActivity. It can be opened two ways. 1 - by MainActivity and 2 - by going to my app's URL
In the doInBackground(..) of AsyncTask in FooActivity I have the following code to finish this activity.
Intent returnIntent = new Intent(FooActivity.this.getApplicationContext(), MainActivity.class);
returnIntent.putExtra("result", "one");
AcceptActivity.this.setResult(FooActivity.this.RESULT_OK, returnIntent);
AcceptActivity.this.finish();
This works fine when my app had already been opened. Because when I finish FooActivity I would see MainActivity.
However, if my app was not already opened and the first time it opened was through a URL, then FooActivity just finishes and I see the phone's desktop.
Question
Is there a way to detect whether MainActivity has been opened already or not? If it is not opened then I'd change above code to open the MainActivity.
Just do that in your onPostExecute() of your FooActivity
launch home screen using startActivity(Intent) with an intent that has flag FLAG_ACTIVITY_CLEAR_TOP, and pass the result manually through the intent
Related
Intent intent = new Intent('some system ui activty not in my application');
startActivity(intent);
I delay a certain time then jump back to myself activity, However the SystemActivity is still back in history. How to finish it? I tried nohistory ,excludeFromRecents and start my activity with clearTop, but nothing can be done.
The problem is, if the SystemActivty go to some other page by user, I cannot use Intent to jump to the first page of SystemActivity, which maybe make user confused.
If you want the systemActivity to end before returning to the mainActivity try
startActivityForResult instead of startActivity
startActivityForResult(intent, 0);
I have an Intent which starts/open an activity/window that is triggered by an alarm. Everything works fine, however the Main activity/window of the Application is also open behind the first activity referenced above when triggered. Thus, when this activity is dismissed (with the help of a button), the Main window of the Application is displayed on the device. How can I open my activity without opening the Main activity of the Application? Basically, the user shound't be seeing the Main activity of the Application upon dismissing the activity triggered by the alarm. Please note that I am not explicitly calling the Main activity upon opening the specific activity mentioned above. Is this normal behaviour, or am I doing something wrong?
The specific activity is launched with the following lines:
Intent intentRead = new Intent(context, myActivity.class);
intentRead.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intentRead);
Problem
Your code starts a new Activity in a new task :
Intent intentRead = new Intent(context, myActivity.class);
intentRead.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intentRead);
The Flags make sure that the new activity gets its own task and the activity will not be launched if it is already running at the top of the history stack.
Now, suppose your MainActivity is already in the background(recent apps) and alarm is triggered which starts your myActivity. If no instance of myActivity is present then a second task is created for it. Else if an instance is present then the all activities on top of myActivity in the second task will be removed.
Solution
In your case, as you want to clear any other activities present you should use :
Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TASK
If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.
Suppose the first activity is started by your main activity, could you just finish main activity after starting the first activity. I mean like this:
startActivity(MainActivity.this, FirstActivity.class);
MainActivity.this.finish();
I'm new in android development, and there is something about the life cycle activity that I don't understand, especially with the following example of application that i'm working on.
In my app, I have a Login activity and a Main activity.
In my Login activity, with successful attempt, there is a Intent that start the main activity, and finish() the login activity.
==> There, my login activity is destroyed, so this should not show up again.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("authentResult", tokenDto);
startActivity(intent);
finish(); //destroy activity to not open it with back button`
In my Main activity, I have a disconnect button that create an Intent that start a (new ?) login activity.
==> Until there, everything's normal, and the login activity is displyed.
Intent loginActivity = new Intent(this, LoginActivity.class);
startActivity(loginActivity);
In the login activity, using the Back button should close the app.
To do that, I send an intent with special flag to the main activity to finish it (So the back button will not wake up the main activity), and then I finish the login activity. The onDestroy method is called and I see the login window close itself.
==> From here I expect the app to be closed. But a "new" login activity shows up, and i suspect that it would be the activity of the first point, so I'm a little lost there...
public void onBackPressed() {
Log.d(TAG, "BACK PRESSED - loginActivity");
//Finish MainActivity
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
finish(); // finish login activity
}
In the onCreate in the mainActivity, I begin with this :
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Do anyone could explain to me what I'm missing, or show me a better way to close the app directly ?
Don't hesitate to telle me if something's not clear.
If you declare the Login activity as main activity in the Manifest, if you don't destroy it when you launch the second activity then i think the back button will do all you expect without any additional code, and if you press back key on the login activity it will go to phone home screen
On Android applications is the system that decides when to close/kill application.
I have one activity which can be launched from several other activites, along with url filter intents.
On this activity I use the home icon in the actionbar as a back button, bringing the user back to the previous activity (and not as a "home" action). For now I do this by calling the finish() function. This works fine when working from within the application.
However, if launching the activity by an url filter intent, I want the home icon to bring the user to the main activity. Obviously, calling finish() will just close the activity.
So my question is, is there a way to check whether my application stack is empty and then launch the main acivity if true? Or am I attacking this the wrong way?
If your app is launched via url intent filter and it creates its own task, then you can use
if (isTaskRoot()) {
// This activity is at root of task, so launch main activity
} else {
// This activity isn't at root of task, so just finish()
}
EDIT: Added another possible method
If your app is launched into an existing task when it is launched via URL intent filter, then you can do something like the following:
When you launch your activity from other activities in your application, add an EXTRA to the Intent like this:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("internal", "true");
startActivity(intent);
When your activity gets launched it can then check for the presence or absence of the EXTRA in the Intent to determine whether it was launched internally or via URL intent-filter, like this:
Intent intent = getIntent();
if (intent.hasExtra("internal")) {
// Launched internally
} else {
// Launched via intent-filter
}
In my application, the first activity takes care of the splash screen/loader and onces it completes the load, a second activity comes up or starts.. Now when a person clicks on back or navigates back, it is supposed to exit the application but it goes tthe loader or splash activity..how can i avoid this ?? Hope my question was clear...
When your application opens second screen aka second activity finish the first activity aka splash screen.
Just use finish() method to achieve this.
Here is an example
Intent intent = new Intent();
intent.setClass(context, Second.class);
startActivity(intent );
//call finish
finish();
when you start second activity after then CurrentActivity.finish() current activity in your case like splash...