0

With the code below, I can open applications such as: Google Maps, calculator and retain their state when they pass to foreground:

  Intent i;
            PackageManager manager = getPackageManager();
            try {
                i = manager.getLaunchIntentForPackage("com.google.android.apps.maps");

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

                i.setAction("android.intent.action.VIEW");

                startActivity(i);

            } catch (PackageManager.NameNotFoundException e) {

            }

My problem is with an application created by me, because my application does not maintain the state. She is always restarted.

I have tried to use the properties in the manifest: launchMode, alwaysRetainTaskState, always unsuccessful.

Thanks in Advance

Paulo Dias
  • 113
  • 8

2 Answers2

1

The android activity has different stages like Pause and resume which is called when an activity is paused. If the foreground activities stopped , the app may even visible in an paused state. In such cases you need to override your onPause() methods to say to your activity what to do if it paused.

Add the logic you need to perform when your activity paused in the onPause() method.

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first

    do some task here...
    if (connectServer) {
     pauseDownloadData();
    }
}

when you resume your activity, it will invoke onResume()

public void onResume() {
    super.onResume();  // Always call the superclass method first
    do some task here...
    if (connectServer) {
        resumeDownloadData(); // Local method to handle camera init
    }
}
BDRSuite
  • 1,594
  • 1
  • 10
  • 15
  • Thanks for your help. I understood, but I have to configure something so that the application maintains state? – Paulo Dias Oct 22 '14 at 14:39
  • if I minimize the Google Maps app, and the move to foreground, it shows the last activity, before enter in background. – Paulo Dias Oct 22 '14 at 14:44
  • In my application, onDestoy method is always called when I try to resume (when execute the code in the first comment) – Paulo Dias Oct 22 '14 at 14:49
  • 1
    basically if you press your back button, the onDestroy() method will be called and the activity will be alive and only flushed away if there is low memory or you finished it in program. So, save your application activitie's state in onPause() method or onStopped() and in the onResume() method resume your activities. this link can be much useful http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – BDRSuite Oct 22 '14 at 15:28
  • thanks for your time. I thought there was a solution like the same way that an application behaves when click the "Home button" and from "Task Manager" choose my application, she goes to Foreground, keeping the state it was before going to background. In this case in this case the onDestoy is not called. – Paulo Dias Oct 22 '14 at 16:04
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