0

我的应用程序内部添加了一个popup activity,该应用程序在我的应用程序启动15秒后弹出。但是当我打开另一个activity并回到我的main activity弹出窗口再次显示。我希望它只在用户第一次打开应用程序时出现。我应该做什么改变?感谢帮助。仅在应用程序启动后显示弹出式动作

这里是弹出代码:

Handler handler = new Handler(); 

    handler.postDelayed(new Runnable() { 

     @Override 

     public void run() { 


     if (context != null) { 

       AlertDialog.Builder alert = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle) 
         .setTitle("Title") 
         .setMessage("Message") 
         .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // continue with delete 
           Intent intent = new Intent(MainActivity.this, WebActivity.class); 
           startActivity(intent); 
          } 
         }) 
         .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialogInterface, int which) { 
           dialogInterface.dismiss(); 
           // do nothing 
          } 
         }); 

       mDialog = alert.create(); 
       mDialog.getWindow().getAttributes().windowAnimations = R.style.MyAlertDialogStyle; 
       if (!((Activity) context).isFinishing()) 
       mDialog.show(); 
       // .setIcon(R.drawable.inr1) 
       // .show(); 

      } 
     } 
    }, 15000); 
+0

您可以在sharedPrefrances中为此维护一个布尔变量。 – NaserShaikh

+0

你只需要一次动作或者每当用户打开应用程序时 – koutuk

+1

解雇你在'onResume'上弹出 – Mohit

回答

1

你可以做到这一点,如果它需要弹出仅在应用程序启动

public class MyApplication extends Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 

     //Set some flag on global constant 

    } 
} 
0

节省布尔内sharedpreferences读取它的值,并确定全天候运行第一次或没有。

这里是帮助你的代码。

public void firstimeRun(boolean value) { 
    SharedPreferences sharedPreferences = getPrefrenceManager(); 
    sharedPreferences.edit().putBoolean("key", value).apply(); 
} 


public boolean isRunningForthefirstTime() { 
    SharedPreferences sharedPreferences = getPrefrenceManager(); 
    return sharedPreferences.getBoolean("key", false); 
} 


private SharedPreferences getPrefrenceManager() { 
    return PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
} 
+0

我应该在哪里包含这段代码? –

+0

在对话前的第一个活动... – koutuk

+0

在您的MainActivity中调用这些方法,通过从带有条件代码的sharedprefs读取这些值来保存该值并显示对话框。 –

0

保存sharedPrefrances该标签(计数器)当应用程序启动时的解释是第一次应用程序启动

0

可以在这样的应用程序类使用全局变量。

public class global extends Application 

// some Boolean variable to hold status 

并确保你把这个类中的Android应用程序表现标签

android:name 
0

在MainActivity添加以下代码:

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

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    if(sharedPreferences.getBoolean("IS_FIRST_TIME", true)) { 
     //show your dialog here 
     //... 
     //change the value of your sharedPreferences 
     sharedPreferences.edit().putBoolean("IS_FIRST_TIME", false).apply(); 
    } 
} 
0

请检查活动的生命周期。

注意:删除处理程序。 注2:创建方法并调用对话框 注意3:检查生命周期中的以下方法。

onPause(): 作为活动生命周期的一部分,当活动进入后台但尚未(尚未)终止时调用。与onResume()的对应部分。当活动B在活动A前面启动时,此回调将在A上被调用。在A的onPause()返回之前,B不会被创建,所以一定不要在这里做任何冗长的事情。

相关问题