仅启动 Activity 一次(第一次使用应用程序)使用布尔标志

launching Activity only once(First app use) Using Boolean flags

所以我得到了 firstScreen 我想在第一次使用应用程序时显示。 我有我的 mainActivity,它将跟随 firstScreen。 我只从 android 开始,我不想使用这里提供的解决方案:How to launch activity only once when app is opened for first time? AND Can I have an android activity run only on the first time an application is opened? 因为我还不知道 SharedPreferrences

那么我怎样才能使用布尔标志实现呢?

我有:boolean firstTimeUse = false; 在我的 firstScreenActivity

当我启动 myMainActivity 时,我将标志设置为 true;

firstTimeUse = true;
Intent intent = new Intent(FirstScreen.this,MainActivity.class);
startActivity(intent);

问题是 MainActivity 无法识别布尔变量。 我做错了吗?或者我仍然可以进行一些修改并使用标志来完成?

编辑 FirstScreen.java:

package com.example.predesignedmails;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class FirstScreen extends AppCompatActivity
{
    TextView welcomeTextTextView;

    String welcomeText;

    ImageButton goToMainImageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_screen);

        viewsInitialization();

        welcomeText = "Welcome to Pre Designed Mails.\n"
                + "In here you will have a tons of Emails templates for about every purpose you will need.\n"
                + "Just fill the small details and click Send.\n\n"
                + "Send E-mails fast and efficient!";

        welcomeTextTextView.setText(welcomeText);
        welcomeTextTextView.setMovementMethod(new ScrollingMovementMethod());

        goToMainImageButton.setOnClickListener(new View.OnClickListener()
        {   
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent(FirstScreen.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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

        if (SettingsManager.getBoolean(this, SettingsManager.FIRST_LAUNCH, true))
        {
            SettingsManager.saveBoolean(this, SettingsManager.FIRST_LAUNCH, false);

            // First launch code
            Log.d("FirstLaunchCheckUp","First Launch");
        }
    }

    private void viewsInitialization()
    {
        welcomeTextTextView = (TextView) findViewById(R.id.welcome_text_text_view_id);
        goToMainImageButton = (ImageButton) findViewById(R.id.go_to_main_button_id);
    }
}

我手动添加的onResume()方法。当我在 Eclipse 中创建一个新的 activity 时,它没有自动添加。

MainActivity.java:

package com.example.predesignedmails;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity 
{
Button hatefullMailButton;
Button loveMailsButton;
Button welcomeScreenButton;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color)); // Setting background color

    viewsInitialization();

    hatefullMailButton.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(MainActivity.this,HatefulMailsActivity.class);
            startActivity(intent);
        }
    });
    loveMailsButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(MainActivity.this,LoveMailsActivity.class);
            startActivity(intent);
        }
    });
    welcomeScreenButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(MainActivity.this,FirstScreen.class);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void viewsInitialization()
{
    hatefullMailButton = (Button) findViewById(R.id.hateful_email_button_id);
    loveMailsButton = (Button) findViewById(R.id.love_email_button_id);
    welcomeScreenButton = (Button) findViewById(R.id.welcome_screen_button_id);
}

}

在你的Activity

@Override
protected void onResume() {
    super.onResume();
    if (SettingsManager.getBoolean(this, SettingsManager.FIRST_LAUNCH, true)){
        SettingsManager.saveBoolean(this, SettingsManager.FIRST_LAUNCH, false);
        //your first launch code
    }
}

SharedPreference 助手 class

public class SettingsManager
{
    public static final String FIRST_LAUNCH= "first_lauch";

    public static String getString(Context context, String key, String defValue) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(key, defValue);
    }

    public static int getInt(Context context, String key, int defValue) {
        return PreferenceManager.getDefaultSharedPreferences(context).getInt(key, defValue);
    }

    public static boolean getBoolean(Context context, String key, boolean defValue) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defValue);
    }

    public static void saveString(Context context, String key, String value) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).commit();
    }

    public static void saveInt(Context context, String key, int value) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(key, value).commit();
    }

    public static void saveBoolean(Context context, String key, boolean value) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).commit();
    }
}

以后你可以在这个 SettingsManager 上写更简单的方法,比如

public static int getFirstLaunch(Context context) {
    return getBoolean(context, FIRST_LAUNCH, true);
}

public static int saveFirstLaunch(Context context, boolean value) {
    return saveBoolean(context, FIRST_LAUNCH, value);
}

并像

一样使用它
@Override
protected void onResume() {
    super.onResume();
    if (SettingsManager.getFirstLaunch(this)){
        SettingsManager.saveFirstLaunch(this, false);
        //your first launch code
    }
}

之所以firstScreen无法识别mainActivity中的boolean firstTimeUse,是因为它还不存在。只有执行完startActivity(intent)行后,才会存在classmainActivity的对象。基本上,您不能在尚不存在的事物上设置布尔值。

相反,您可以将额外的信息传递给要启动的 activity。当 just-started-activity 正在初始化自身时,它可以读取额外信息并对其进行操作。

因此,为您想要开始的 activity 构建您的意图,并在 'extras':

中设置额外信息
Intent intent = new Intent(FirstScreen.this,MainActivity.class);
intent.putExtra("firstTimeUse", true);
startActivity(intent);

现在要让您的 MainActivity 知道 firstTimeUse 的值,它必须阅读额外内容:

public class MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean firstTimeUse = getIntent().getBooleanExtra("firstTimeUse", false);
        // do processing dependent on whether it's the first time or not
    }
}

这应该可以解决您的 "the MainActivity doesn't recognize the Boolean variable" 问题。