androidandroid-fragmentsandroid-activityandroid-manifestandroid-homebutton

Application not resumed and again start from Main Activity if opened from Home Or Background


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 ?


Solution

  • 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