resuming activity in Android using Service - android

I have a mainActivity.
When starts, it starts a service and bonds it.
There is a timer which will send the mainActivity (this) to back after X seconds, while the service keep running and listening, i use moveTaskToBack (true).
When the service listener triggered, the service starts the activity, but instead of the activity to be called through onResume() (since it was sent to back) its called through onCreate(), to call the activity currently i use :
Intent dialogIntent = new Intent(getBaseContext(), MainActivity.class);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
getApplication().startActivity(dialogIntent);
Which explains why the activity is created all over again.
I tried using FLAG_ACTIVITY_REORDER_TO_FRONT but got an exception.

add this attribute in activity of AndroidManifest.xml android:launchMode="singleInstance" in AndroidManitest.xml
AndroidMenifest.xml
<application>
<activity
android:launchMode="singleInstance">
</activity>
</application>

Related

Why my activity reCreate and not reStart

i start my activity by this
Intent window = new Intent(mContext, popup.class);
window.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mContext.startActivity(window);
my activity was started and the log
onCreate
onStart
onResume
now i want to stop my activity i use this
moveTaskToBack(true); //i don't know if this best way to stop an activity
and the Log
onPause
onStoped
onDestroy not called
now i want to reStart same activity i use
Intent window = new Intent(mContext, popup.class);
window.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
mContext.startActivity(window);
here its not restart the activity its make new one
onCreate
onStart
onResume
and this is my activity in manifest
<activity
android:name=".activity.popup"
android:taskAffinity=".MyDialog"
android:configChanges="orientation|screenSize"
android:exported="true"
android:label="#string/app_name"
android:theme="#style/Theme.FloatingWindow.Popup"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
tools:ignore="ExportedActivity"
>
</activity>
note ** if i use launchMode singleInstance its restart and not create new activity but here problem i cant Instance same activity for different data
Dont add this flag Intent.FLAG_ACTIVITY_NEW_TASK and also add this tag to your activity in manifest android:launchMode="singleInstance"

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.

android : How to work around the fact that a singleInstance activity cannot expect to rely onActivityResult when starting subActivity?

I am making an android application, which has the following execution flow:
A service registers a PendingIntent with the AlarmManager
When the alarm is launched, a Receiver receives the intent, and (given some conditions) calls startsActivity() for my Main Activity, which in the manifest has been declared as android:launchMode="singleInstance". Note that for this call to work, the intent passed should have an Intent.FLAG_ACTIVITY_NEW_TASK
When started, Main Activity modifies itself a bit, and calls startActivityForResult for an Activity, which we'll call WebviewActivity (because it contains a webview, but that's besides the point)
When the user is done interacting with theWebViewActivity, setResult() and finish() are called on it, and one would expect for MainActivity.onActivityResult() to be called.
But of course this does not happen, as has been documented in many discussions here, the reason apparently being that an Activity launched from a singleInstance Activity, runs in a different Task.
A solution I think would be to have the WebActivity start the MainActivity instead.
The question is, is there a way to maintain onActivityResult being called at the right time? In that case, which aspects from the starting point of the execution flow should change?
Please note that MainActivity should not have multiple instances at the same time (it is basically an interface to the service) but if its launchMode is set to standard, the Receiver, because of the FLAG_ACTIVITY_NEW_TASK that is required, will do just that.
Manifest declaration of MainActivity:
<activity android:name=".activities.MainActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
android:uiOptions=”splitActionBarWhenNarrow”
</activity>
Receiver launches MainActivity by calling
onReceive(Context context, Intent intent)
{
intent.setClass(context, MainActivity.class);
int flag = Intent.FLAG_ACTIVITY_NEW_TASK;
intent.setFlags(flag);
context.startActivity(intent);
}
I use the following workaround for this problem:
Activity A is the caller
Activity B is the singleInstance activity from which I want the result
In activity A I register a broadcast receiver as following
PickReceiver receiver=new PickReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("ActivityA_pick");
registerReceiver(receiver,filter);
class PickReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("ActivityA_pick")){
//get data from intent extras
}
}
In ActivityB when it's time to send the data I use:
sendBroadcast("ActivityA_pick").putExtra("...data...");
finish();
This way i can get the result I want when I want a result from one of my own activities. If you want a result from the system or another app you can adjust this using a dummy activity that doesn't have launch mode singleInstance, have it start the activity for result and when it gets it onActivityResult it sends the broadcast to the caller.
Hope this helps
As the Main Activity is a single instance, is doing what it has been told.
So yes, you have to start the Main Activity from the Web Activity in order to be coherent with the tasks executions

Alarm to the same active activity

I've set an alarm to start an activity, say A.
If the intended A activity is not in the foreground, A will wake up and hit onResume(), where I check to see the source of it.
But what if A is IN the foreground, what happens to my intent ?
Thanks !
It will depend how the launchmode is defined for activity A. For instance, if it's set to standard:
<activity android:name=".Activity" android:launchMode="Standard">
it will spawn a second activity when the intent is fired, and spawn as many activities as there are intents, where's if it's set to "singleTop":
<activity android:name=".Activity" android:launchMode="singleTop">
it will simply route the intent to the instance of the activity that's already running. There are two more types: "singleInstance" and "singleTask", so see the documentation for more details in order to customize as you wish.

Start Activity in a BroadcastReceiver

I've built a small app. The only thing it does is, catch an outgoing call and show some activity when it happens. There is just an Activity and a BroadcastReceiver.
I wanted to integrate my code with another application, I removed the BroadcastReceiver from the Manifest.xml and created (and registered) it dynamically from the main activity. My receiver fired well but the activity is not shows up.
What is the difference between the two methods?
How can I make the activity to show up?
from MainActivity.java:
callInterceptor = new InterceptOutgoingCall();
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
callInterceptorIntentFilter.setPriority(100);
registerReceiver(callInterceptor, callInterceptorIntentFilter);
and from the function receiver.onReceive(Context,Intent):
Intent alertIntent = new Intent(context, AlertActivity.class);
alertIntent.putExtra("callnumber", phonenbr);
alertIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertIntent);
my activity is declared in the manifest like this:
<activity android:name=".AlertActivity"
android:screenOrientation="portrait"/>
I found the answer at two threads:
Android launch an activity from a broadcast receiver
Activity started from notification opened on top of the activity stack
In the manifest, the activity should be declared with android:taskAffinity.
And when starting the intent I had to add a flag = Intent.FLAG_ACTIVITY_NEW_TASK

Categories

Resources