[英]First installation by Activity: how to destroy that?
我必須在安裝應用程序后實施一項活動來捕獲首選項。 此活動必須僅顯示一次,第一次。
在清單中,我有這個:
<activity
android:name="com.example.android.Setting"
android:label="@string/settings_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
但是我必須在安裝后銷毀它嗎?
您應該創建一個StartActivity來讀取首選項並確定首先顯示哪個Activity:
public class StartActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = null;
// If the user is running the app for the first time
if(getSharedPreferences("FirstRunCheck", Context.MODE_PRIVATE).getBoolean("is_first_run", true)) {
// Set the target activity to settinga
i = new Intent(this, SettingsActivity.class);
}
else {
// Set the target activity to your main screen
i = new Intent(this, MainScreenActivity.class);
}
// Start the activity
startActivity(i);
// Close the StartActivity
finish();
}
}
public class MainActivity extends Activity {
try
{
//in first run app go to catch and other go to try only
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
String x= pref.getString("login", null);
edit.commit();
if(x.equals("first"))
{
//add your code here
}
}
catch(Exception e)
{
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("login","first");
edit.commit();
Intent intent = new Intent(getApplicationContext(), First.class);
startActivity(intent);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.