1

What is the best way to launch an application from another?

I'm doing:

Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage("XXX");

        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);

        startActivity(i);

and I can start the application.

My problem is that if the application is running, it must maintain state, showing the last activity, without launching the application from the beginning (SplashScreen).

If I try to start the application by clicking the "Home button" and from "Task Manager" choose my application, she goes to Foreground, keeping the state it was before going to background.

I tried using different flags without success. The settings should be doing in my application that calls or should be in the application to be started?

Thanks in advance for your help.

Paulo Dias
  • 113
  • 8
  • this was answered for c#, but: [you might want to look at this](http://stackoverflow.com/questions/7522228/saving-state-between-program-restarts) –  Oct 17 '14 at 10:05
  • 1
    I sometimes realy think ppl just downvote posts labled 'android' (I however cant help any further, sorry) – JBA Oct 17 '14 at 10:06
  • Please specify why you are calling another app ...so that i can answer according to purpose – koutuk Oct 17 '14 at 10:13
  • You may wan to have a look at this : http://stackoverflow.com/questions/3872063/launch-an-application-from-another-application-on-android – vkm Oct 17 '14 at 10:16
  • I plan to open my application from another, which may be in the background or not – Paulo Dias Oct 17 '14 at 10:30
  • My purpose is to test the behavior of my application in this situation, because it is done in my client. This code is used by another application to open my application. The aim is to set up my application to this situation if possible. – Paulo Dias Oct 17 '14 at 10:38

1 Answers1

0

Problem solved, with the solution below. Now I can start my application, if it is closed or resume (resume in any activity).

The code that starts the application is (Launcher):

Intent i;
    PackageManager manager = getPackageManager();
    try 
    {
        i.addFlags(0);
        i.setPackage(null);

        if (i == null)
            throw new PackageManager.NameNotFoundException();

        startActivity(i);

    } 
    catch (PackageManager.NameNotFoundException e) 
    {
    }

My application that starts, has the following manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Main" >

           <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>

        <activity android:name=".Screen2"
            android:label="@string/screen2Title"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout" 
            android:windowSoftInputMode="adjustResize" 
             android:launchMode="singleTask" >
           </activity>  
        <activity android:name=".Screen3"
            android:label="@string/screen3Title"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout" 
            android:windowSoftInputMode="adjustResize" 
             android:launchMode="singleTask"  >

        </activity>

    </application>

</manifest>

Now the application is resumed to any activity, without ever navigate to the root activity.

Paulo Dias
  • 113
  • 8
  • I neither understood your question nor your answer. What you've changed to get it working? By the way, if this is the correct answer you also should accept it as follows. – tato.rodrigo Oct 24 '14 at 11:01
  • This is the same problem with more information: http://stackoverflow.com/questions/26532206/launch-an-application-from-another-application-resumes-always-to-root-activity – Paulo Dias Oct 24 '14 at 15:11