How can I call a different xml on the second call of my onResume in Android Eclipse project

冷暖自知 提交于 2020-01-25 17:49:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!