1

I am working on an android app and have an activity. I have written a code in my activity that will start a new activity after getting response from server, this code is getting executed even after I press back button on my activity.

So, I want to check that if my current activity is not active anymore, then the code should not run.

How can I check that activity is not running or in existence any more.

Please help me if anyone know how to do this.

Thanks a lot in advanced.

Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

4 Answers4

0

Activity is still in memory that's why your code is executed to finish it completed call finish() after starting another activity. To check if current activity is there or not you have to override onDestroy() method which is called everytime when your activity is completely destroyed. For checking activity is running or not follow this question

Community
  • 1
  • 1
Mahesh Giri
  • 1,810
  • 19
  • 27
0

just call finish() method when you starts a new Activity like

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();//this activity has been finish and the code will not execute

you can check if Activity is destroyed or not. override this method

public void onDestroy() {
super.onDestroy();
Log.d("Activity name,"destroyed");
}
Devendra Singh
  • 2,343
  • 4
  • 26
  • 47
0

Try like this

class MyActivity extends Activity {
 static boolean isActive = false;

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

  @Override
  public void onStop() {
     super.onStop();
     isActive = false;
  }
}
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
-1

Check here : Proper way to know whether an Activity has been destroyed

The question have your answer and as the solution provided just use the SharedPrefrence to store the variable.

Community
  • 1
  • 1
Android Geek
  • 8,956
  • 2
  • 21
  • 35