2016-01-16 22 views
1

我有2個活動。首次運行應用程序時,我必須在第一次啓動應用程序時打開活動1中的彈出窗口。之後,我想參加活動2並在那裏做一些改變。再次回到活動2,我不想打開彈出窗口。但問題是,每當我回到第一活動彈出窗口是打開的。如何解決這個問題?如何在Android上使用onResume()?

這是我的代碼。

db = dbhelper.getReadableDatabase(); 
     String query = "SELECT * FROM Inspector where ActiveStatus= '1' AND FollowFlag ='1'"; 
     Cursor cursor = db.rawQuery(query, null); 
     if (cursor.moveToFirst()) 
     { 
      do 
      { 
       String strInspectoreName = cursor.getString(cursor.getColumnIndex("Inspector_name")); 
       String strInspectorId = cursor.getString(cursor.getColumnIndex("Inspector_Id")); 

       if(!strInspectorId.equals(str_LoginUserId)) 
       { 
        inspector_ArrayList.add(strInspectoreName); 
        Log.e("Post ", " Total FollowUp Users !!!" + strInspectoreName); 
       } 

      } while (cursor.moveToNext()); 
     } 
     cursor.close(); 
     db.close(); 
     int countFollowUp = inspector_ArrayList.size(); 
     Log.e("Post ", " Total countFollowUp Users !!!" + countFollowUp); 

     if(countFollowUp == 0) 
     { 
      final Dialog dialog = new Dialog(CustomActionActivity.this); 
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
      dialog.setContentView(R.layout.custom_dialog_layout); 

      Button followStart = (Button) dialog.findViewById(R.id.button_FollowStart); 
      followStart.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent i = new Intent(CustomActionActivity.this, Filter_Screen.class); 
        startActivity(i); 

       } 
      }); 

      Button dismissButton = (Button) dialog.findViewById(R.id.button_Dissmiss); 
      dismissButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialog.dismiss(); 
       } 
      }); 
      dialog.show(); 
     } 

這裏是我的onResume()在活動第一次

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

     Log.e(" Activity ", " Resume !!! "); 
     Log.e("From ", " Filter 222"); 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     booleanValue_one = sharedPreferences.getBoolean("LISTVIEW_EVENT_ONE", false); 
     booleanValue_two = sharedPreferences.getBoolean("LISTVIEW_EVENT_TWO", false); 
     Log.e("", "booleanValue_one=" + booleanValue_one + " booleanValue_two=" + booleanValue_two); 


     if (booleanValue_one == true || booleanValue_two == true) 
     { 
      GetAllActivityDetails task = new GetAllActivityDetails(); 
      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
      Log.e(" booleanValue_one "," = " + booleanValue_one +" After completing async task !!!!"); 
      updatedDownLoadStatus(); 
     } 

     if (booleanValue_one == false && booleanValue_two == false) 
     { 
      populateList(); 
     } 

    } 

這裏是我的第二個活動onBackPressed() - >去第一個活動

@Override 
public void onBackPressed() { 
     super.onBackPressed(); 
     Intent a = new Intent(Filter_Screen.this, CustomActionActivity.class); 
     a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     finish(); 
     startActivity(a); 
     overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom); 
} 
+0

onBackPressed()方法不啓動活動。 –

+0

然後如何返回第一個活動。 –

+1

@override public void onBackPressed(){super.onBackPressed(); overridePendingTransition(R.anim.slide_in_bottom,R.anim.slide_out_bottom); } –

回答

0

進行布爾重點boolean key=true;和使用該布爾鍵在onResume()onCreate()中顯示Popup使布爾值爲假

onResume()也呼籲當活動開始,當你回來的時候它再次調用

0

你onResume()被稱爲每次你回去運行的活動,也創建活動第一次這就是爲什麼它西港島線運行代碼evrytime你回到活動。 要解決此問題,您應該將onResume中的代碼放入onCreate中,這種方式僅在首次創建活動時運行。

欲瞭解更多信息,並充分了解活動檢查的生命週期:http://developer.android.com/training/basics/activity-lifecycle/starting.html#lifecycle-states

+0

我不明白。 –

+0

@ p.ld好,如果我正確理解你的問題,你希望彈出窗口只在第一次啓動應用程序時才能打開,那是正確的嗎? –

0

它,這是運行的東西就像一個引進的拳頭時間收集一些用戶信息,保存狀態,以共享偏好,並用它來相應地檢查並允許或禁止彈出窗口。