finish not closing activity opened by a separate application - android

ExternalApp opens my android application app. Now in my app, I have a button that returns a value to the external app then closes itself so whatever screen in externalapp opened my app is on top.
while no error is thrown, my application's screen remains on top and I cant see if the value has been sent to the calling external app:
public void sendBackData() {
Intent intent = new Intent();
intent.putExtra("value", "user");
setResult(RESULT_OK, intent);
finish();
}
I have tried this.finish() but it made no difference. Basically, after button click calls sendBackData method, I want the application to pass the value user to the calling external application and hide/close the screen.

Related

Handling intent when receiving application is in the background

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.

Marshmallow, starting SMS app via Intent and pressing back end up on home screen instead of in calling application

I'm calling SMS app with following code:
public static void startSmsIntent(Activity context, String value) {
Intent smsIntent = new Intent( Intent.ACTION_SENDTO, Uri.parse("smsto:"+value) );
try{
context.startActivity(smsIntent);
}
catch( ActivityNotFoundException e ){
}
}
It opens conversation thread in default SMS app. After pressing back button insteaad of returning to calling application, Default SMS app conversation list is displayed. Pressing back again, and I'm ending on home screen and only way to get back to my app is via Recent apps chooser. Below Marshmallow it works as expected, first back brings me to calling application screen. I tried to use extra flag:
smsIntent.putExtra("finishActivityOnSaveCompleted", true);
which was mentioned in different context in the documentaiton - here, but no luck.

Application button to open current/new instance of another app

I have two applications, A and B. In app A I've added a button which opens app B.
When the button is clicked I want to open App B if it's not already running, otherwise I want to bring the app to the front.
This is the code I use:
Intent intent = getPackageManager()
.getLaunchIntentForPackage(
"com.myapp.something");
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
The code seems to work, but the problem is that if I open app B from app A (using this code) and then click directly the icon of App B, I get two instances of the app, which is undesired.
How can I open the app, as the code does, but also get the same instance even if the app's icon is clicked?
Add Intent.FLAG_ACTIVITY_CLEAR_TOP

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.

Prevent SingleTask app from closing when returning to the calling app

I have two apps, which I will refer to as app A and app B. App B is a single task app and normally, if you start this app from the Home screen and use the Back button, you will not be allowed to return to the Home screen. Now suppose app A needs to call app B using an Intent. If the user then uses the Back button in app B, I really do want them to return to app A. But since I am overriding the Back button, it is not possible to return to app A using the Back button.
How can I return to app A but make sure app B remains running if it was running when app A called it? If app B was not running when app A called it, app B should shutdown (get destroyed) when the Back button is pressed.
I am using Android 2.2
UPDATE:
I tried the following:
#Override
public void onBackPressed()
{
try
{
if (this.returnToClient)
moveTaskToBack (true);
this.returnToClient = false;
}
catch (Exception ex)
{
}
}
I set returnToClient to true if the activity is launched by a calling app by checking some bundle data that gets passed in.
Now if I press the Back button, app B will move to the background and app A comes to the foreground. Looks good. Now if I press the Home button and then launch app B, app B just picks up where it left off. Also good. Now the bad part. If I do a long press on the Home button to bring up the history list and then tap on the icon for app B, the onNewIntent method gets called exactly the same with and with the same data being passed in as though app A had initiated the activity. So what I am guessing is that Android treats launching an app from the Home screen different than it does from the History list. Why? I have no idea. It seems that the history has to do with whoever launched the activity last and persists the bundle data as part of that history. That is weird and it results in unwanted behavior making app B look like it just got called from app A.
Have AppA launch AppB using an Intent with an EXTRA that indicates that it was launched from AppA, something like this:
Intent intent = new Intent(this, AppB.class);
intent.putExtra("returnToAppA", "true");
startActivity(intent);
Then, when AppB traps the "back" key, it can check if it was launched from AppA and if so, it can return to appA like this:
Intent intent = new Intent(this, AppA.class); // Use the starting
// (root) Activity of AppA here
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Setting FLAG_ACTIVITY_NEW_TASK will ensure that a new instance of AppA will not be created, but it will just return to the existing task containing AppA.

Categories

Resources