简体   繁体   中英

when don't keep activities selected on Setting , progress dialog is not dismissing

when "don't keep activities" is selected on Setting's Developer Options progress dialog is not dismissing. Actually, I am displaying the Progerss bar,for initialize the Application, When I am going to Activity A to Activity B and then came to Activity A, the Progress bar is not dismissing after initialize completed. Below is my Code,

First Activity

package com.example.donotkeepactivitiesalive;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    public static final int MESSAGE_PROGRESS_BAR_INITIALIZING = 1;
    private Dialog m_cObjDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println(getClass().getName() + "before InitializeEnvironment >>>>>>");
        new InitializeEnvironment().execute();
        System.out.println(getClass().getName() + "after InitializeEnvironment >>>>>>");
        Button lb = (Button) findViewById(R.id.button1);
        lb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent lIntent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(lIntent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        m_cObjDialog = new ProgressDialog(this);
        m_cObjDialog.setTitle("Initializing");
        ((ProgressDialog) m_cObjDialog).setMessage("Initializing the Application");
        m_cObjDialog.setCancelable(false);
        return m_cObjDialog;
    }


    class InitializeEnvironment extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            System.out.println(getClass().getName() + "onPreExecute >>>>>>");
            super.onPreExecute();
            showDialog(MESSAGE_PROGRESS_BAR_INITIALIZING);
        }

        @Override
        protected String doInBackground(String... aurl) {
            System.out.println(getClass().getName() + " doInBackground >>>>>>");
            return null;
        }

        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(MESSAGE_PROGRESS_BAR_INITIALIZING);
            System.out.println(getClass().getName() + " onPostExecute >>>>>>");
        }
    }

}

Second Activity

package com.example.donotkeepactivitiesalive;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

Is there any way to track this problem and work even if "don't keep activities" is enabled

I solved this, by checking the Bundle object is null or not.If the Bundle object is null then call the AsynTask class, else don't call,

if(null == savedInstanceState) {
        new InitializeEnvironment().execute();
    }

But make sure that your previous Activity is not overriding onActivityResult()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM