首先按Activity安装:如何破坏它?

时间:2013-12-02 09:09:11

标签: android android-intent android-fragments android-activity

我必须在安装app之后实现一个活动来捕获首选项。 此活动必须首次显示一次。

在Manifest中我有这个:

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

但我必须在安装后销毁它吗?

2 个答案:

答案 0 :(得分:4)

你应该创建一个 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();
    }
}

答案 1 :(得分:0)

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

    }