Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run code on first OnCreate only

I have a piece of code that I only want to run the very first time a particular OnCreate() method is called (per app session), as opposed to every time the activity is created. Is there a way to do this in Android?

like image 790
AJJ Avatar asked Nov 29 '22 06:11

AJJ


2 Answers

use static variable.

static boolean checkFirstTime;
like image 23
KDeogharkar Avatar answered Nov 30 '22 21:11

KDeogharkar


protected void onCreate(Bundle savedInstanceState) has all you need.

If savedInstanceState == null then it is the first time.

Hence you do not need to introduce extra -static- variables.

like image 89
greenapps Avatar answered Nov 30 '22 21:11

greenapps