Search code examples
androidandroid-activityonresume

Android - Starting a finished parent activity from child activity onResume()


I have a MainActivity that has no view attached to it. This is the launching activity that checks which day of the week it is and starts a new activity for that particular day. I have an activity for each day of the week. After checking the day and starting that day's activity, I call finish() so that the MainActivity gets destroyed (so that when I click on back button blank screen is not displayed and I can come out of the app).

Now the issue that I am facing is --

If I click on home button or app goes to background and if the day changes and if the app comes to foreground again then the previous day's view is getting displayed. It should actually get updated to display the current day's activity.

Since onResume() is called after the app comes back to the foreground, I tried adding the following code in onResume() of each day's activity to start MainActivity again and check for the day and then start that particular activity. But only a blank screen is getting displayed.

Please find the code below:

MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        calendar= Calendar.getInstance();
        int day=calendar.get(Calendar.DAY_OF_WEEK);

        switch(day)
        {
            case Calendar.SUNDAY: {
                Intent intent=new Intent(MainActivity.this, SundayTabActivity.class);
                startActivity(intent);
                break;
            }
            case Calendar.MONDAY: {
                Intent intent=new Intent(MainActivity.this, MondayTabActivity.class);
                startActivity(intent);
                break;
            }
            case Calendar.TUESDAY: {
                Intent intent=new Intent(MainActivity.this, TuesdayTabActivity.class);
                startActivity(intent);
                break;
            }
            case Calendar.WEDNESDAY: {

                Intent intent=new Intent(MainActivity.this, WednesdayTabActivity.class);
                startActivity(intent);
                break;
            }
            case Calendar.THURSDAY: {

                Intent intent=new Intent(MainActivity.this, ThursdayTabActivity.class);
                startActivity(intent);
                break;
            }
            case Calendar.FRIDAY: {

                Intent intent=new Intent(MainActivity.this, FridayTabActivity.class);
                startActivity(intent);
                break;
            }
            case Calendar.SATURDAY: {
                Intent intent=new Intent(MainActivity.this, SaturdayTabActivity.class);
                startActivity(intent);
                break;
            }
        }
        finish();
    }

SaturdayTabActivity onResume() method

@Override
    protected void onResume() {
        super.onResume();
        Calendar calendar= Calendar.getInstance();
        int day=calendar.get(Calendar.DAY_OF_WEEK);
        if (day != 6)
        {
            System.out.println("day changed-----");
            Intent intent=new Intent(SaturdayTabActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

Please help me understand if this is the right approach or if I am missing anything.


Solution

  • Add launchMode="singleTask" for the MainActivity in AndroidManifest file..

     <activity
        android:name=".MainActivity"
        android:launchMode="singleTask"/>