Start an Activity with a parameter

Put an int which is your id into the new Intent. Intent intent = new Intent(FirstActivity.this, SecondActivity.class); Bundle b = new Bundle(); b.putInt(“key”, 1); //Your id intent.putExtras(b); //Put your id to your next Intent startActivity(intent); finish(); Then grab the id in your new Activity: Bundle b = getIntent().getExtras(); int value = -1; // or other … Read more

Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

UPDATE: onActivityCreated() is deprecated from API Level 28. onCreate(): The onCreate() method in a Fragment is called after the Activity‘s onAttachFragment() but before that Fragment‘s onCreateView(). In this method, you can assign variables, get Intent extras, and anything else that doesn’t involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be … Read more

Android activity life cycle – what are all these methods for?

See it in Activity Lifecycle (at Android Developers). onCreate(): Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one. Always … Read more

Null pointer Exception – findViewById()

findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that’s what happening to you. Note that if you don’t setContentView(), and don’t have a valid view to findViewById() on, findViewById() will always return null until you call setContentView(). This also means variables in the top-level … Read more