Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity lifecycle issue

Tags:

android

I have two activities named Home and Cart. When I press a button in activity Home it goes to activity Cart.

For that I used the following Code

 public void onClick(View v) {
     Intent intent = new Intent(this, Cart.class);
     startActivity(intent);
 }

In Cart activity when I press a button it comes to activity Home.

For That

 public void onClick(View v) {
     Intent intent = new Intent(this, Home.class);
     startActivity(intent);
 }

Activities are changing fine but the onCreate method calls every time the activity starts.

Is that a fine life cycle of Activity or not? If not, how can I solve this?

like image 850
user703834 Avatar asked Sep 10 '26 07:09

user703834


1 Answers

How to solve what? That is the intended behavior of activities. Nevertheless, if you do not want that the methods in your onCreate method are called, you could use a flag like this:

public class YourActivity extends Activity {

   private boolean firstStart = true; 

   public void OnCreate(...) {

      if (firstStart) {
         //do your stuff here
         firstStart = false;
      }

   }
}