简体   繁体   English

通过Activity进行首次安装:如何销毁它?

[英]First installation by Activity: how to destroy that?

I have to implement an activity to catch the preferences just after the installation of app. 我必须在安装应用程序后实施一项活动来捕获首选项。 This activity must showing only one time, the first time. 此活动必须仅显示一次,第一次。

In the Manifest I have this: 在清单中,我有这个:

 <activity
        android:name="com.example.android.Setting"
        android:label="@string/settings_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

But I have to destroy that after installation? 但是我必须在安装后销毁它吗?

You should make a StartActivity which reads the preferences and decides which Activity to show first: 您应该创建一个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.

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