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.