mcCat
mcCat

Reputation: 120

android onCreate(Bundle savedInstanceState)

I am trying to pass an object to an activity through Intent, and as I'm debugging, I noticed that apparently I never even get into the onCreate() method of the activity. Even though the activity starts, I have the layout and everything, I don't get a chance to debug, because the breakpoint inside the onCreate() method is never reached. How is this possible, what am I not understanding here? Help :)

    listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


            String selection = itemsArray[groupPosition][childPosition];
            Class calc = null;

            switch (selection) {
                case "Calc1":
                    calc = Calc1.class;
                    break;
                case "Calc2":
                    calc = Calc2.class;
                    break;
            }

            Intent intent = new Intent(Main.this, calc);
            intent.putExtra("controller", getController());
            startActivity(intent);

            return false;
        }
    });

The activity is started on a OnChildClick event, the object I'm trying to pass is the controller (returned by getController() method)

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calc1);

    controller = (Controller) getIntent().getSerializableExtra("controller");
    setController(controller);

    editA = (EditText) findViewById(R.id.editA);
    editB = (EditText) findViewById(R.id.editB);
}

This is the onCreate(), I set the breakpoint at setContentView(), and like I said, apparently I never get there... I feel like I'm missing something here.

Btw. I'm brand new to android ;)

Thanks for reading!

Upvotes: 2

Views: 517

Answers (2)

Alex Hong
Alex Hong

Reputation: 281

When set break point, did you run in Debug Mode ?

To run in debug mode

try menu Run > Debug 'app' (Alt + Shift + D)

To run in release mode

try menu Run > Run 'app' (Alt + Shift + X)

If you are running in release mode, when application started, to start debug, choose menu Run > Attach debugger to Android process > choose your process then go to your activity you want to debug again.

Upvotes: 1

JCLaHoot
JCLaHoot

Reputation: 1054

The Android activity lifecycle can be a bit tricky at first. Here's a diagram that explains it fairly well: https://github.com/xxv/android-lifecycle and you can get a lot more detail on all the activities here: https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)

It's possible that the activity goes straight to onResume or onRestart, but if you want to find out for sure (and this is a great way to learn more about the activity lifecycle!) put some logs in a bunch of the different activity methods (onCreate, onPause, onRestart, etc...). Then try things like navigating between the different activities, killing the app, pressing the home button, locking your screen, rotating the screen, etc. You'll notice that the flow is not exactly the same, and it will help you figure out where to put your code in your particular use case. Hope this helps!

Upvotes: 1

Related Questions