0

I have this activity which is launched by the AlarmManager at some specific time. What I want to do is, if the user taps on Stop button, the activity will go to its parent activity only if the parent activity is active (already launched in the foreground) but not if not launched or launched in the background, in the last case, the subActivity will just finish.

I have done this in the manifest file:

<activity
     android:name=".subActivity"
     android:parentActivityName=".MainActivity" />
<activity

I used a trick I found here to check if the MainActivity is active or not, but it seems to not work:

class MainActivity extends Activity {
     static boolean isActive = false;

      @Override
      public void onStart() {
         super.onStart();
         isActive = true;
      } 

      @Override
      public void onStop() {
         super.onStop();
         isActive = false;
      }
}

And this in the subActivity class:

public class subActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      //Full screen
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
      setContentView(R.layout.subActivity);

      Button stopBtn = findViewById(R.id.stopBtn);
      stopAlarmBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Context context  = getBaseContext();
              if (MainActivity.isActive) {
                  startActivity(new Intent(context, MainActivity.class));
              }
              finish();
        }
    });
}

Thank you for the help.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
  • How can your `MainActivity` be in the foreground when your `AlarmManagerActivitiy` is? – kalabalik Dec 22 '17 at 19:49
  • @kalabalik I mean that the user, while using MainActivity, the system' AlarmManager fires up SubActivity – hiddeneyes02 Dec 22 '17 at 21:17
  • From the [docs](https://developer.android.com/guide/components/activities/activity-lifecycle.html#onstop) about onStop(): "When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen." In other words, your `MainActivity` is never "active" when `AlarmManagerActivity` is visible. – kalabalik Dec 22 '17 at 21:27
  • @kalabalik Ok, that was the problem, but how to resolve it? – hiddeneyes02 Dec 23 '17 at 16:13
  • What is the problem you want to solve? Why do you want to go the the parent activity only if it has been launched? – kalabalik Dec 23 '17 at 16:33
  • @kalabalik Because I don't want him to lose any work he was doing in the MainActivity, because of the Activity launched by the AlarmManager – hiddeneyes02 Dec 23 '17 at 17:54
  • The recommended solution for this problem is [`onSaveInstanceState`](https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras). – kalabalik Dec 23 '17 at 18:00

0 Answers0