2

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!

mcCat
  • 120
  • 2
  • 13
  • Add a log or a toast inside the callback method to know if you reach it. – Alexandre Martin Jun 26 '16 at 22:07
  • is the activity declared in manifest? have u tried putting debugger at controller = (Controller) instead? – Muhammad Umar Jun 26 '16 at 22:08
  • Well, it must be reached at some point because the activity opens (which is only possible if `setContentView(R.layout.calc1);` gets called), but I don't understand why I can't debug. yes everything is in the manifest. I added a Toast after `setController(controller);` but it doesn't appear. Also yes I tried putting debugger at other places in the method. It never works. – mcCat Jun 26 '16 at 22:12

2 Answers2

1

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!

JCLaHoot
  • 1,034
  • 2
  • 13
  • 21
  • Thank you so much! I will definitely read that :) I know the lifecycles are a bit confusing at first, which is why I'm playing around with it and try to debug a lot. In this case I just didn't (and still don't) understand how my debugger won't start at all, even though the activity is running. Maybe I'll find out after reading the links you posted ;) Thanks again! – mcCat Jun 27 '16 at 00:44
1

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.

Alex Hong
  • 281
  • 1
  • 11