0

当用户第一次运行它时,我的应用程序中会显示一个教程

        Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
         public void run() { 
             if (firstRun == true) {


                 Intent tut = new Intent(MainActivity.this, Tutorial.class);
                 startActivity(tut);
                 firstRun = false;

             }
         } 
    }, 200);

我推迟了它,因为没有延迟我只是黑屏(界面没有时间加载)

但是这样做我多次打开Tutorial.class,我做错了什么?

编辑:

这里还有一些代码,我不会全部粘贴,因为阅读时间太长而且与问题无关

我这样保存我的偏好

 @Override
protected void onStop(){
   super.onStop();

  // We need an Editor object to make preference changes.
  // All objects are from android.context.Context
  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putInt("Counter1", counter);
  editor.putInt("Counter2", counter2);
  editor.putBoolean("FirstRun", firstRun);
  editor.putString("Label1", label1S);
  editor.putString("Label2", label2S);

  editor.commit();
}
protected void onPause(){
    super.onPause();

   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   SharedPreferences.Editor editor = settings.edit();
   editor.putInt("Counter1", counter);
   editor.putInt("Counter2", counter2);

   editor.commit();
 }

这是我在 onCreate() 中恢复它们的方法;

// Restore previous settings and data


    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    int counterRestored = settings.getInt("Counter1", 0);
    int counter2Restored = settings.getInt("Counter2", 0);
    boolean firstRunRestored = settings.getBoolean("FirstRun", true);
    String label1Restored = settings.getString("Label1", "Counter 1");
    String label2Restored = settings.getString("Label2", "Counter 2");
    counter = counterRestored;
    counter2 = counter2Restored;
    firstRun = firstRunRestored;
    label1S = label1Restored;
    label2S = label2Restored;
    renameLabel();
    calculateTotal();

这是我的第二个活动 Tutorial.class

public class Tutorial extends MainActivity{

ImageButton  btnSkip, btnSkip2, btnNext, btnNext2;
RelativeLayout tutorial, tutPage1, tutPage2;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tutorial);


    btnSkip = (ImageButton) findViewById(R.id.btn_skip);
    btnNext = (ImageButton) findViewById(R.id.btn_next);
    btnSkip2 = (ImageButton) findViewById(R.id.btn_skip2);
    btnNext2 = (ImageButton) findViewById(R.id.btn_next2);
    tutorial = (RelativeLayout) findViewById(R.id.tutorial);
    tutPage1 = (RelativeLayout) findViewById(R.id.page1);
    tutPage2 = (RelativeLayout) findViewById(R.id.page2);


    btnSkip.setOnClickListener(new View.OnClickListener(){
        @Override
         public void onClick(View v) {
            finish();
        }
    });
    btnSkip2.setOnClickListener(new View.OnClickListener(){
        @Override
         public void onClick(View v) {
            finish();
        }
    });
    btnNext.setOnClickListener(new View.OnClickListener(){
        @Override
         public void onClick(View v) {
            tutPage1.setVisibility(View.GONE);
            tutPage2.setVisibility(View.VISIBLE);
        }
    });
    btnNext2.setOnClickListener(new View.OnClickListener(){
        @Override
         public void onClick(View v) {
            tutPage1.setVisibility(View.VISIBLE);
            tutPage2.setVisibility(View.GONE);
            tutorial.setVisibility(View.VISIBLE);
            finish();
        }
    });

}

}

4

2 回答 2

1

你为什么不试试

 if(firstRun){
     firstRun = false;
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
         public void run() { 
             Intent tut = new Intent(MainActivity.this, Tutorial.class);
             startActivity(tut);
         }
     } 
     }, 200);
 }
于 2012-08-10T19:35:04.833 回答
0

的值firstRun不会在多次运行中持久存储。

您应该将此值存储在SharedPreferences中,以便即使在应用程序关闭后它仍将保留其值。您可以在此处找到有关如何使用 SharedPreferences 的教程。

于 2012-08-10T20:45:02.607 回答