簡體   English   中英

僅在Android首次啟動時顯示設置屏幕

[英]Showing the setup screen only on first launch in android

我正在制作一個Android應用程序,但我無法弄清楚如何讓設置屏幕只顯示第一次。 這就是應用程序的工作方式:用戶在安裝后啟動應用程序,並顯示歡迎/設置屏幕。 一旦用戶完成設置,除非用戶重新安裝應用程序,否則設置屏幕將永遠不會再出現。

我怎么能讓這件事發生? 請提前幫助和謝謝!

使用SharedPreferences測試它是否是第一次啟動。

注意:以下代碼未經過測試。

在你的onCreate中(或者你想要根據首次啟動而做什么),添加

// here goes standard code 

SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);

if(pref.getBoolean("firststart", true)){
   // update sharedpreference - another start wont be the first
   SharedPreferences.Editor editor = pref.edit();
   editor.putBoolean("firststart", false);
   editor.commit(); // apply changes

   // first start, show your dialog | first-run code goes here
}

// here goes standard code

做一個幫助活動。 這將是你的啟動器活動。它不會包含任何布局,它只會檢查應用程序的第一次新運行。 如果它將首先運行,則將啟動安裝活動,否則將啟動MainActivity。

public class HelperActivity extends Activity {

    SharedPreferences prefs = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Perhaps set content view here

        prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
    }

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

        if (prefs.getBoolean("firstrun", true)) {
            // Do first run stuff here then set 'firstrun' as false
            //strat  DataActivity beacuase its your app first run
            // using the following line to edit/commit prefs
            prefs.edit().putBoolean("firstrun", false).commit();
            startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
            finish();
        }
        else {
        startActivity(new Intent(HelperActivity.ths , MainActivity.class));
        finish();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM