如何在Android上使用onResume()?

时间:2016-01-16 07:13:46

标签: android android-activity activity-lifecycle

我有2个活动。第一次运行应用程序时,我必须在第一次启动应用程序时打开活动1中的弹出窗口。之后我想去活动2并在那里做一些改变。我再次回到活动2,我不想打开弹出窗口。但问题是每当我回到第一个活动弹出窗口打开时。如何解决这个问题?

这是我的代码。

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

这是我在活动1中的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() - >转到第1活动

@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);
}

3 个答案:

答案 0 :(得分:0)

创建一个布尔键boolean key=true;并使用该布尔键在onResume()onCreate()中显示弹出窗口使该布尔值为假

onResume()也会在活动开始时调用,当您再次调用时再调用

答案 1 :(得分:0)

每次回到正在运行的活动时都会调用onResume(),并且第一次创建活动时,为什么它会运行代码evrytime,然后返回活动。 要解决此问题,您应该将onResume中的代码放在onCreate中,使其仅在首次创建活动时运行。

有关详细信息并完全了解活动检查的生命周期:http://developer.android.com/training/basics/activity-lifecycle/starting.html#lifecycle-states

答案 2 :(得分:0)

这是第一次运行的事情,如收集一些用户信息的介绍,将状态保存到共享首选项,并使用它来相应地检查和允许或禁止弹出窗口。