【问题标题】:show activity only once with Onclick button使用 Onclick 按钮仅显示一次活动
【发布时间】:2017-10-27 18:40:55
【问题描述】:

我正在尝试使用 Onclick 按钮仅显示一次活动 首先我有一个活动 useheadphone 包含button 当用户点击这个button ma​​inactivity 将启动

我的 useheadphone.java :

public class useheadphone extends Activity {
    Button skip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_useheadphone);
        final SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        hide();
        skip = (Button)findViewById(R.id.skip);
        skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pref.getBoolean("activity_executed", false)){
                    Intent toMain = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(toMain);
                    finish();
                } else {
                    SharedPreferences.Editor ed = pref.edit();
                    ed.putBoolean("activity_executed", true);
                    ed.commit();
                }
            }
        });
    }

我的清单

<activity
            android:name=".useheadphone"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_useheadphone"
            android:screenOrientation="portrait"
            android:theme="@style/FullscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

【问题讨论】:

  • 只有当activity_executed为真时才会被设置为真。您需要从它为 false 时将其设置为 true,反之亦然。
  • @MikeT 非常感谢,请给我更多信息
  • @ S.Queroane:: 根据您在 Doron 的回答中留下的评论,在我看来,您正在向后设计您的应用程序。如果您希望 useheadphone 只出现一次,则让 MainActivity 启动活动并仅有条件地打开 useheadphone 活动,即当“activity_executed”为真时。
  • @Barns52 谢谢,但 Xenolion 的回答对我有用stackoverflow.com/questions/46981516/…
  • 有效的方法并不总是好的。学习愉快!

标签: android android-activity sharedpreferences


【解决方案1】:

onCreate方法(dont delete the original I have just copied and paste)底部的onCreate方法内添加此代码。:

if(pref.getBoolean("activity_executed", false)){
                Intent toMain = new Intent(this,MainActivity.class);
               startActivity(toMain);
}

您还应该考虑在此处使用较小的上下文:

更改此行:

Intent toMain = new Intent(getApplicationContext(), MainActivity.class);

收件人:

Intent toMain = new Intent(useheadphone.this, MainActivity.class);

【讨论】:

  • 没有进入下一个活动?
  • 是的,但是当我单击跳过按钮应用程序仅显示一次 mainactivity 时,我想要的问题是,当我关闭应用程序并再次运行它时,我想显示 mainactivity,而不是 headphone 活动
  • 我已经编辑了我的答案!现在问题应该解决了!先卸载应用
  • 很高兴您的问题得到了解决! 编码愉快!
【解决方案2】:

我认为它可能应该是这样的(如果需要,您可以添加 else):

skip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(pref.getBoolean("activity_executed", false)){
                SharedPreferences.Editor ed = pref.edit();
                ed.putBoolean("activity_executed", true);
                ed.commit();
                Intent toMain = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(toMain);
                finish();
            }
        }
    });

【讨论】:

  • 非常感谢,但是当我关闭我的应用并再次运行它时仍然无法正常工作仍然显示第一个活动useheadphone
  • 此答案为您提供了在 onClick 方法中设置 SharedPreferences 的正确方法。 (我唯一的建议是将getApplicationContext 更改为userheadphone.this)。
【解决方案3】:

当用户单击按钮(暂时跳过)并将其重定向到第二个活动时,此代码将仅显示一次主要活动。

关于 Main Activity

onCreate 方法
SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCE", MODE_PRIVATE);
String FirstTimeInstall = sharedPreferences.getString("FirstTimeInstall", "");

    if (FirstTimeInstall.equals("Yes")) {
         startActivity(new Intent(MainActivity.this, SecondActivity.class));
    }

在您的 onClickListener 上到 Main Activity 上的按钮

skipBtn.setOnClickListener(view -> {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("FirstTimeInstall", "Yes");
            editor.apply();
            startActivity(new Intent(MainActivity.this, SecondActivity.class));
        });

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多