2013-04-30 124 views
0

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

当我在我的主要活动创建它它不解雇本身当我点击“好”,这将创建一个新的活动。所创建的新活动从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(); 

    } 
} 

回答

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(); 
     } 
+0

感谢。但这并不奏效。当我点击“确定”按钮时,新的活动被创建,但对话结束。每当我切换或创建一个新的活动时,即使我没有做任何事情来触发它,对话也会被触发。 – localhost 2013-04-30 14:23:06

+0

@PeterMerrill您是否尝试过将这些说'onStart'或'onResume'只是为了看看会发生什么?... – TronicZomB 2013-04-30 14:31:57

+0

感谢您的建议@TronicZomB。我确实尝试过,没有任何改变。 – localhost 2013-04-30 14:43:07

0

从你的代码有一点了解,我的建议是保持当你想对话的检查显示,当你不。例如,您可以使用静态布尔标志showDialog,根据用途在您的活动中将其设置为true/false。

if(savedVersionCode == appVershionCode && showDialog) 

if(savedVersionCode == appVershionCode && !showDialog) 

它更多地超过程序化解决方案的程序性问题。这是方法只是一个建议。由于您正在遵循singleTon结构类型,所以您必须确定要进一步携带的方法。

第二条本办法可能是,不要做这种方式。你想在你的活动来实现的常用方法是与SharedPref检查,所以为什么不:

  1. 创建延伸活动类。
  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扩展你的类。 e.g

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. 
    } 
    } 
+0

你介意给我一个如何使用你的建议的例子吗? – localhost 2013-04-30 14:41:07

+0

@彼得梅里尔,请参阅更新的答案 – 2013-04-30 15:34:09

+0

谢谢你。有没有办法可以使用我在这个例子中编写的一些代码? – localhost 2013-04-30 15:46:55