1

After Login I start My Application.When Application is at another Activity or Fragment and I press Home Button application goes to background.But suppose again If started from Home or Background it start from first Splash and Login screen.It is not resuming to Activity or Fragment from where it is put to background.
This is code for ActivityMain manifest

<application
        android:name="app.AppController"
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@drawable/app_icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Splash"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        <activity android:name=".ActivityTwo"></activity>
    </application>  

This is code for Splash activity

public class Splash extends AppCompatActivity {
    private Handler handler;
    private Runnable r;
    RelativeLayout relativeLayout;
    ConnectivityManager connec;
    TextView isOffline;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        relativeLayout=(RelativeLayout)findViewById(R.id.relative);
        isOffline=(TextView)findViewById(R.id.offline);
        connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        isInternetOn();
        handler=new Handler();
        r = new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(Splash.this, MainActivity.class));
                finish();
            }
        };
    }  

I have finish the Splash Activity after checking connection so what is the problem

 private void isInternetOn() {
        if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED)
        {
            r = new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(Splash.this, MainActivity.class));
                    finish();
                }
            };
            // if connected with internet take Action
        } else if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED)
        {
            isOffline.setVisibility(View.VISIBLE);
            Snackbar snackbar=Snackbar.make(relativeLayout,"No internet connection",Snackbar.LENGTH_INDEFINITE).setAction("Close", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            snackbar.show();
        }

How to achieve this so that application should be start from where it was put to background ?

Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

3 Answers3

1

Try this:

    .....
    setContentView(R.layout.activity_splash);
    if (!isTaskRoot()) {
        finish();
        return;
    }
    relativeLayout=(RelativeLayout)findViewById(R.id.relative);
    //your code

Use this code in splash screen. It will resolve your problem

Ankit
  • 309
  • 2
  • 8
  • I have Login Code on Main Activity.If I apply this code for Splash Activity then what about Main Activity – Satyam Gondhale Apr 25 '18 at 05:51
  • after apply this code, your application will start from the point where you have gone to background but if the OS destroy it than it will start from Splash – Ankit Apr 25 '18 at 05:53
  • Is there any code I need to add so that OS will not destroy the Activity.Because my sequence of Activity is Splash, Main Activity(Login), Second Activity where I am adding and replacing Fragments further. – Satyam Gondhale Apr 25 '18 at 05:56
  • isTaskRoot: Return whether this activity is the root of a task. The root is the first activity in a task. – Ankit Apr 25 '18 at 05:57
  • My root Activity is Second Activity ? – Satyam Gondhale Apr 25 '18 at 05:58
  • OS kills the application when it becomes low in memory otherwise not. I think there is no code for this. When you start the application first time, at that time Splash is the root. Try the code – Ankit Apr 25 '18 at 06:01
  • So I need to add this `isTaskRoot` in Splash only ? Is it ? – Satyam Gondhale Apr 25 '18 at 06:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169720/discussion-between-ankit-and-satyam). – Ankit Apr 25 '18 at 06:03
0

Yes, for that after login finish your login activity and go to your particular home screen activity. Because if you are not finishing login activity then when app comes from the background it starts an activity which is in BackStack.

Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
0

Check in your device Don't keep activities option should be disable:

Go To:

Settings >> Developer options >> Don't keep activities, should be disable (If not make it disable and check your app).

Deven
  • 3,078
  • 1
  • 32
  • 34