How to launch once instance of an Activity? - android

I am having a scenario. I am trying to lock my application using an PinActivity which I created. I am running a service which has a counter timer for 5mins. If there is no activity by the user in app for 5 mins. I will show him the PINActivity which he has to unlock and enter the app. I am launching the PINActivity like this:
Intent loginIntent = new Intent(this, PINActivity.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(loginIntent);
So when the user comes back to the app and see the PINActivity he will unlock it and enter the app. But what if the user doesn't enter the PIN and leave the app again ideal for more then 5 mins(Note: my service starts the counter timer the moment user brings the app to foreground). I don't want to add multiple instance of the same PINActivity at top. How can I make sure I have only one PINActivity at the top?

You should use FLAG_ACTIVITY_SINGLE_TOP. This will prevent Android from launching PINActivity if there is already an instance of PINActivity on top of the stack.
Intent loginIntent = new Intent(this, PINActivity.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_SINGLE_TOP);
startActivity(loginIntent);
Please don't try to use special launch modes like singleTask or singleInstance as others have suggested. This won't help and will complicate the problem for you.
You can specify android:launchMode="singleTop" in the manifest entry for PINActivity as well. This has the same effect as FLAG_ACTIVITY_SINGLE_TOP.

Add launchMode="singleInstance" in your activity in manifest like this:
<activity
android:launchMode="singleInstance"
android:name=".MainActivity"
..... />

add android:launchMode="singleInstance" in manifest
<activity
android:name=".MainActivity"
android:launchMode="singleInstance"/>

When you launch activity you can use this :
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
If activity is already there then it will use the same instance else create new one and no duplication will be there.

In manifest add attribute of singleInstance in activity tag.
<activity
android:launchMode= "singleInstance" />
Click Here

Related

Single android application with two tasks and its navigation

I am developing a calling application. My HomeActivity is a singleTask activity. My call activity is also a singleTask activity.
From HomeActivity a call is initiated. At this moment, there are two tasks for my application as they both are singleTask. In call screen I have a button to reach my HomeActivity.
When I press the home button in my call screen and navigate back, my call activity is destroyed. But it should not get destroyed. It should remain.
When I press home button in call screen I do the below.
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setClass(this.getActivity(), MyHomeActivity.class);
startActivity(intent);
My manifest declaration:
<activity
android:name=".XXX.MyHomeActivity"
android:label="#string/app_name"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustNothing"
android:theme="#style/MyTheme"
>
</activity>
<activity
android:name=".XXX.MyCallActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/MyTheme"
android:windowSoftInputMode="adjustResize">
Inflating HomeActivity from CallActivity:
Intent intent = new Intent();
intent.setClass(this.getActivity(), HomeActivity.class);
startActivity(intent);
Inflating CallActivity:
Intent intent = new Intent();
intent.setClass(this, CallActivity.class);
startActivity(intent);
Can anyone help me with the navigation parameters to use to achieve this?
In order to have 2 tasks, you need to make sure that the root Activity of the tasks have different taskAffinity, otherwise Android will put both in the same task. The default taskAffinity is the name of your package.
Add android:taskAffinity="" to one of the <activity> declarations.
According to the documentation using FLAG_ACTIVITY_CLEAR_TASK will do the following:
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.
So if want your CallActivity to stay in the back stack you can simply do this:
Intent intent = new Intent();
intent.setClass(this.getActivity(), MyHomeActivity.class);
startActivity(intent);

Start another app activity removing it from recents list

I'm trying to start an activity of an app (that is not mine and which code I don't know and/or can't edit), from my app.
What I want to do, is that after starting that activity (let's call it OtherAppActivity) neither my activity (let's call it MyAppActivity) nor OtherAppActivity remain shown in the recent apps list.
My current code:
AndroidManifest.xml
<activity
android:name=".activities.Launch"
android:noHistory="true"
android:exported="true"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="#style/EmptyActivity">
MyAppActivity.java
// Intent to launch OtherAppActivity
Intent intent = new Intent();
intent.setClassName(PKG, ACTIVITY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, 88);
I have done a lot of search, but none of the answers I have found, have worked for me.
I hope you understand what I want to do, and can help me. Thanks in advance.
For your app, try adding android:excludeFromRecents="true" to your application tag in the manifest file and for the intent try setting this flag:
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

Strange issue with opening app

I have LoginActivity where after successful logging I start MainActivity via intent and finish LoginActivity.
I press back button and then open app via icon and It shows me MainActivity but if I open app from recent apps list after pressing back button so I see LoginActivity
I've checked if LoginActivity was destroyed
How can It be?
manifest
<activity android:name=".LoginActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTask" />
start MainActivity
Intent intent = new Intent(getActivity(), MainActivity.class);
mProgressDialog.dismiss();
startActivity(intent);
getActivity().finish();
Remove android:launchMode="singleTask"
Why you are adding launchMode, adding this you will be able to get its instance only once. Let default be "Standard", for more information, please have a look at the documentation.
Docs say:
The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
So, you wouldn't need singleTask launch mode. Apart from this, I can't see <intent-filter> for your LoginActivity as being MAIN action and LAUNCHER category.

Could an activity with single task flag be cleared via clear top?

Assume that I have a launcher activity A which has singleTask launch mode. Now imagine that A starts Activity B like;
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, REQ_ACCOUNT_ACTIVITY);
In this case it seems like A's not destroyed. I wonder if it's correct or did I something wrong?
You can achieve this by using below attribute in AndroidMenifest file
android:finishOnTaskLaunch="true"
<application
...
>
<activity
android:finishOnTaskLaunch="true"
android:launchMode="singleTask"
...>
</activity>
</application>
If you not want to kept new activity in the history stack. Use below one_
FLAG_ACTIVITY_NO_HISTORY
//Actvity B is not in BackStack if we set FLAG_ACTIVITY_NO_HISTORY flag
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, REQ_ACCOUNT_ACTIVITY);
As soon as the user navigates away from Activity B, the activity is finished. This may also be set with the noHistory attribute.
For more -> Tasks and Back Stack
It's been a while but I came across my own question when I was wandering over here.
The activity A was not getting deleted because the flags are set for the activity B. And when I call this code, there's no such activity called B in the task stack already. So effectively, nothing to clear on top of B.
Hope this helps.

Activity is getting created again instead of resuming and using instance from stack?

I have an android application with several activities .Each and every activity has Application Icon in action bar which helps user to return back to main activity directly instead of pressing back button.My problem is that when I use the icon to start my home activity it does not uses the previous instance from the stack and start creating it again.
My Action bar app icon code is :
startActivity(new Intent(this, DashBoard.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
this above code starts Dashboard activity and calls its both onCreate() and onResume().But If I uses back button to return to this activity from any activity it just calls onResume().
Activity definition from manifest file:
<activity
android:name=".DashBoard"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
Why is this happening?Am I missing something to prevent it from not creating it again?Please help
Thanks
Use setFlags(), instead of addFlags(). You are on right track. Use the following code.
Intent intent = new Intent(this, DashBoard.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Remove the FLAG_ACTIVITY_CLEAR_TOP.

Categories

Resources