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

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

我制作了一个 Android 应用程序,我想在我的 MainActivity.java 中制作我的 onResume 方法的计数器 calls 以便在 onResume 期间:

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

2) 第二次调用 setContentView(R.layout.layout2);

根据我寻找的文档,我应该创建一个静态变量,每次调用 onResume 时该变量都会增加。

请问我该如何实施? 提前谢谢你。

也许是这样的(粗略的模板)

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

您可以使用 sharedpreference 来保存计数器。当调用 onResume() 时,您可以在 sharedpreference 中读取计数器(onResume() 的第一行),并且可以保存 onResume() 函数的计数器最后一行。 因此你可以改变 setContentView(R.layout.layout1) 根据 counter 是 double 还是 single.