0

如何防止我的对话框扩展活动?

当我在我的主要活动中创建它时,当我单击“确定”时它不会自行关闭,这会创建一个新活动。创建的新活动从 MainActivity 扩展。

我正在使用共享首选项来确定在用户打开应用程序时将其发送到哪里。我不确定这是否会影响这种情况。

我想阻止对话框扩展 MainActivity。它不应该出现在我创建的其他活动中。

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  SharedPreferences sharedPreferences = getSharedPreferences("version", 0);
  int savedVersionCode = sharedPreferences.getInt("VersionCode", 0);
  int appVershionCode = 0;

  try { appVershionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; }
  catch (NameNotFoundException nnfe) { Log.w(TAG, "$ Exception because of appVershionCode : " + nnfe); }   

  if(savedVersionCode == appVershionCode){

      // Returning user
      Log.d(TAG, "$$ savedVersionCode == appVershionCode");

      // Temporary Navigation
      final Builder alertDialogBuilder = new Builder(this);
      new AlertDialog.Builder(new ContextThemeWrapper(getBaseContext(), android.R.style.Theme_Dialog));
      alertDialogBuilder.setTitle("Temporary Navigation");
      alertDialogBuilder.setMessage("Go to the new activity.");
      alertDialogBuilder.setPositiveButton("Okay", new OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
           Log.d(TAG, "$$ onClick");
              Intent newactivity = new Intent(MAINACTIVITY.this,NEWACTIVITY.class);
              startActivity(newactivity);
              dialog.cancel();
          }
      });
      alertDialogBuilder.show();
      // End

  } else {

      // First time visitor
      Log.d(TAG, "$$ savedVersionCode != appVershionCode");

      // Hide graphics meant for returning users
      ((Button)findViewById(R.id.Button01)).setVisibility(View.INVISIBLE);

      SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
      sharedPreferencesEditor.putInt("VersionCode", appVershionCode);
      sharedPreferencesEditor.commit();

      Builder alertDialogBuilder = new Builder(this);
      alertDialogBuilder.setTitle("Welcome");
      alertDialogBuilder.setMessage("Click Okay to continue.");

      alertDialogBuilder.setNeutralButton("Okay", new OnClickListener() {

          @Override
          public void onClick(DialogInterface dialog, int which) {
           Log.d(TAG, "$$ onClick");
              Intent leagues = new Intent(MAINACTIVITY.this,NEWACTIVITY.class);
              startActivity(leagues);
          }
      });

      alertDialogBuilder.show();

  }
}
4

2 回答 2

0

dialog.dismiss()像这样尝试:

@Override
      public void onClick(DialogInterface dialog, int which) {
       Log.d(TAG, "$$ onClick");
          Intent newactivity = new Intent(MAINACTIVITY.this,NEWACTIVITY.class);
          startActivity(newactivity);
          dialog.dismiss();
      }
于 2013-04-30T14:19:01.967 回答
0

从对您的代码的一些了解来看,我的建议是检查您何时希望显示对话框以及何时不显示对话框。例如,您可以使用静态布尔标志 showDialog,根据使用情况在您的活动中将其设置为真/假。

if(savedVersionCode == appVershionCode && showDialog)

if(savedVersionCode == appVershionCode && !showDialog)

它更多的是通过程序解决方案来解决程序问题。这种方法只是一个建议。由于您遵循的是singleTon类型的结构,因此您必须确定要进一步执行的方法。

第二种方法可能是,不要这样做。您要在活动中实现的常用方法与SharedPref检查有关,所以为什么不:

  1. 创建一个扩展 Activity 的类。
  2. 在其中添加您的 SharedPref 相关方法。
  3. 现在您可以将该类扩展到您的所有活动。

    public class commonMethod extends Activity{
    
    public void my_sharedPrefMethod(){
    // do some thing with prefs
    }    
    
    @OverRide
    public void onCreate(){
    super.onCreate();
    // common onCreate code for every activity. Recommend not to change this
    // so that you can implement  
    }
    
    // you can also write other methods onPause(), onDestroy() here too.
    {
    

现在您可以使用此类 commonMethod 扩展您的类。例如

    public class main extends commonMethod{

    @overRide
    public void oncreate(){
    }

    @overRide
    public void my_sharedPrefMethod(){
    }

    public void showMyDialog(){
    // this way dialog box would not be shown on every activity but just this one.
    } 
    }   
于 2013-04-30T14:35:44.927 回答