问题
I make an Android app and I want in my MainActivity.java to make a counter of my onResume method calls so that during onResume's:
1) first call to setContentView(R.layout.layout1);
and
2) second call to setContentView(R.layout.layout2);
According to documentation that I seeked I should make a static variable that will be increased for every onResume's call.
How could I implement it please? Thank you in advance.
回答1:
Perhaps something like this (rough template)
public class MainActivity {
public static int contentViewCount = 0;
public void onCreate() { //or perhaps onStart()
contentViewCount = 0;
}
public void onResume() {
if(contentViewCount == 0) {
//set first layout and increment the static counter
setContentView(R.layout.layout1);
contentViewCount++;
} else {
setContentView(R.layout.layout2);
}
}
}
回答2:
you can use sharedpreference for save of counter. when onResume() called you can read counter(first line of onResume()) in sharedpreference and you can save counter last line of onResume() fuction. thus you can change setContentView(R.layout.layout1) according counter is double or single.
来源:https://stackoverflow.com/questions/28966716/how-can-i-call-a-different-xml-on-the-second-call-of-my-onresume-in-android-ecli