简体   繁体   English

我如何在Android Eclipse项目的onResume的第二次调用中调用不同的xml

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

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: 我制作了一个Android应用程序,我想在MainActivity.java中对onResume方法调用进行计数,以便在onResume期间:

1) first call to setContentView(R.layout.layout1); 1)首先调用setContentView(R.layout.layout1);

and

2) second call to setContentView(R.layout.layout2); 2)第二次调用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. 根据我寻求的文档,我应该创建一个静态变量,该变量将为每个onResume的调用增加。

How could I implement it please? 请问如何实施? Thank you in advance. 先感谢您。

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);
        }
    }
 }

you can use sharedpreference for save of counter. 您可以使用sharedpreference保存计数器。 when onResume() called you can read counter(first line of onResume()) in sharedpreference and you can save counter last line of onResume() fuction. 调用onResume()时,您可以在sharedpreference中读取计数器(onResume()的第一行),并且可以保存onResume()功能的计数器的最后一行。 thus you can change setContentView(R.layout.layout1) according counter is double or single. 因此,您可以根据计数器为double还是single来更改setContentView(R.layout.layout1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM