Handling intent when receiving application is in the background - android

There 2 apps: First app can open the second one using:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.my.path");
startActivity(intent);
this works fine.
Second app can call again the first app by using intents with actions. for example:
public void call(String number)
{
Intent myIntent = new Intent(Intent.ACTION_CALL);
myIntent.setData(Uri.parse("tel:" + number);
startActivity(myIntent);
}
And there lies a problem which I'll get to shortly.
I'm handling the received intent in the first app in the onCreate method. The first app is simply a single activity with many fragments which are switched with a fragment transaction. When receiving the intent from the second app, I'm making a transaction to a specific fragment according to the intent.
The problem is that when the first app is not in the background (meaning its close), my handling of the intent works fine. However, if the user opened the second app from the first one and the first one is still in the background, then when a user calls the intent in the second app, the first app is getting back to the foreground but to the same fragment the user was when he launched the second app, while I was expecting to show the new fragment which is based on the user's request in the intent sent from the second app.
Why is this happening and how can I fix it?

You could handle this by overriding the OnNewIntent method handling the intent and bringing to top the required fragment.

Related

startActivity opens last app in background instead of needed activity

I got an app which can receive an event (which is basically a message from a service in the background), and when it does, I am invoking this code:
Intent dialogIntent = new Intent(this, MainActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
This code is supposed to return the app to the front so the user can handle the event.
This code works only if the app I'm working on was also the last app the user has been in. The code doesn't work in these scenarios:
1) The user has paused the app, opened another app and then paused it too so the 2nd app is the last in the background - in which case the last app will always open, even though I'm starting my MainActivity. For example, if I've moved my app to the background, opened Android's settings and then an event is received, the Android's settings screen will open instead of my app.
2) The user is inside another app. In which case, the app won't be brought to the front at all.
How can I made the specific app to be resumed from the background no matter what?
Use launcher category as below:
Intent resumeIntent = new Intent(context, MainActivity.class);
resumeIntent.setAction(Intent.ACTION_MAIN);
resumeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(resumeIntent);
Also add android:launchMode="singleTop" in the manifest of your activity.
You can add android:launchMode="singleTop" in the manifest for the Activity.

Intent data not clearing when user presses back button then recent apps button

I am puzzled over this behavior I am experiencing in an application I am developing...
Short:
Intent data is not clearing out when the user presses the back button to leave the application and then presses the recent button to re-enter the application. (Every other case, the intent data is cleared out)
Long:
I have an application with a splash screen that is used to collect data that is passed in from a URI scheme. I then setup an intent to forward the data to the main activity. The main activity has fragments and is based off the master/detail template.
The intent data is cleared out in all cases, such as pressing the home button and then going back to the application, pressing the recent apps button and then going back to the application, etc. The only case where the intent data is not cleared out is when the user presses the back button and then the recent apps button to get back into the application.
Relevant snippets of code that involve the intents:
// Splash Screen Activity
#Override
protected void onPostExecute(Void result) {
// Data is done downloading, pass notice and app ids to next activity
Intent intent = new Intent(getBaseContext(), ListActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id1", id1);
intent.putExtra("id2", id2);
intent.putExtra("id3", id3);
startActivity(intent);
finish();
}
// ListActivity retrieving intent data
Intent intent = getIntent();
if (intent != null) {
this.id1 = intent.getExtras().getString("id1");
this.id2 = intent.getExtras().getString("id2");
this.id3 = intent.getExtras().getString("id3");
}
// ListActivity clearing intent data
#Override
public void onPause() {
super.onPause();
// Clear intent data
Intent intent = getIntent();
intent.putExtra("id1", "");
intent.putExtra("id2", "");
intent.putExtra("id3", "");
}
I want to note that I have also tried using intent.removeExtra("id1") but that too did not work.
Any idea what is going on? It is as if Android is keeping the old intent even though onPause() is always called to clear the intent data.
actually this is due to Android starting the app from the history hence the intent extras are still in there
refer to this questions Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent
so adding this conditional to handle this special case fixed it for me
int flags = getActivity().getIntent().getFlags();
if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
// The activity was launched from history
// remove extras here
}
I believe the difference here is that the Back key is actually causing your Activity to finish, where as pressing Home causes your activity to be paused, but not finished.So when your process is brought back to the front in the Home case, it is simply resuming an already existing Activity instance, whereas in the Back case, the system is instantiating a new copy of your Activity, calling onCreate(), and handing it a fresh copy of the last Intent recorded for that activity.
In onPause() you are clearing the extras in a "copy" of the Intent. You can try adding
setIntent(intent);
to onPause() after you've cleared the extras (although calling removeExtra() would probably also work instead of setting extras to empty strings).
NOTE:
However, I would suggest that this design is flawed. You shouldn't use the Intent to keep track of state in your application. You should save some state in shared preferences because this will survive your app being killed/restarted, a reboot of the phone, or whatever.
The problem is that the new Intent is not persisted, so that If the user presses the HOME button and your app goes to the background and then Android kills your app because it is not active, when the user returns to the app, Android will create a new process for your app and it will recreate the activity using the original Intent.
Also, if you read the documentation for getIntent() it says that it returns the Intent that started the activity.
To get around the issue I was facing, I opted to use SharedPreferences as a means to pass data between activities.
I know SharedPreferences isn't typically used for this purpose, but it solved my issue and works.

Activity closes in blink after end call

I am calling a activity from my service when an incoming call ends as given below
Intent callIntent1 = new Intent(Intent.ACTION_CALL);
callIntent1.addCategory(Intent.CATEGORY_HOME);
callIntent1.addCategory(Intent.CATEGORY_LAUNCHER);
callIntent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent1.setClass(context, com.example.test.MyActivity.class);
Log.d("TAG", "MyActivity");
startActivity(callIntent1);
but my activity starts for a blink and closes and I see the home screen ,my log shows the call given to MyActivity ,I cannot find the reason for this
I think it is because you are not using your intent categories the right way. If you are explicitly calling the startActivity method, you don't need to add a category to your intent. They are meant to be used with intent filters I guess. If you want to detect something, eg a call, a text etc, add an intent filter to your activity.
Edit: see here

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.

Categories

Resources