5

Well basically, I press a button, this opens up your default camera app by using the camera intent. After a picture is taken, it will save the things needed and redirect to another activity.

In this activity, I have an AsyncTask that can succesfully upload pictures. So what is my problem you may ask. My problem is that it re-creates my activity and therefore reset my ProgressDialog together with it. ( It runs the activity, does the aSyncTask, dies before it can finish it and re-creates my Activity to do the asynctask once again. )

It does not always do this. I think it does this because it changes the Orientation from the phone from Landscape to Portrait. ( I have a Samsung. When I go to the Camera it changes to landscape and when I finish it, it goes back to portrait. )

I've already done my homework and added these things to my manifest:

android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" >

I've made sure to "lock" my app in the portrait orientation but I still see my app change orientation and I believe this is why my activity gets re-created.

I was planning to add all kinds of checks but I believe this is not the right way to handle this situation, since it sometimes does not re-create the activity.

The check I am talking about is to use:

protected void onSaveInstanceState(Bundle outState) {
    outState.putString("started", "1");
}

Anyway, can somebody help me out? I just want it to load the activity without it self-destructing on me.

PS: The VM doesn't have any problems. The VM loads the activity and finishes it without re-creating it.

PPS: Did extra testing, on my Samsung if I keep it on landscape-mode it will work. So it is definately the camera that is destroying my activity with it's orientation change.

Yihka
  • 51
  • 1
  • 4

3 Answers3

9

I had the same issue, turns out you also need to listen for screen size changes in API level 13 or higher as explained here; https://stackoverflow.com/a/11483806

android:configChanges="orientation|screenSize"
Community
  • 1
  • 1
Håkon
  • 131
  • 1
  • 5
2

For this to fix, I had to use following in my manifest file:

android:screenOrientation="portrait"    
android:launchMode="singleTop"    
android:configChanges="keyboardHidden|orientation|screenSize"
artsylar
  • 2,648
  • 4
  • 34
  • 51
0

Try creating a fragment activity to handle displaying and updating the progress dialog

In the fragment activity make sure and set "setRetainInstance(true);" This will make sure it isn't destroyed when the main activity gets created/destroyed.

It's probably a good idea to put the entire image capture process inside this fragment, including the asynctask. Make sure you don't reference the parent activity's context from within the doInBackground() in the AsyncTask. If you do this and the orientation changes (i.e. the activity is destroyed) it will throw an error.

here's a rough example:

public class MyFragment extends FragmentActivity {

    private ProgressBar mProgressBar;
    private boolean mAsyncTaskActive = false;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);

        // grab reference to progress bar
        mProgressBar = (ProgressBar) getActivity().findViewById(R.id.my_progress_bar);


        // check to see if the async task is active and set the progress bar visibility accordingly
        if (mAsyncTaskActive) {
            mProgressBar.setVisibility(View.VISIBLE);
            mProgressBarText.setVisibility(View.VISIBLE);
        }


    }

    // this method is called from your main activity when the user does something (i.e. clicks a button)
    // make sure you have already instantiated the fragment

    public void startSomething() {
        if (mAsyncTaskActive == false) {
            mProgressBar.setVisibility(View.VISIBLE);
            new MyAsyncTask().execute();
            mAsyncTaskActive = true;
        }
    }

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        Context applicationContext;



        @Override
        protected Void doInBackground(String... params) {
            // do stuff
            publishProgress(//some number);
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
        }



}

You should also take a look at how to implement fragments if you're not already familiar. The Android dev blog has a good post on DialogFragments, same priniciples. http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

Aaron
  • 323
  • 3
  • 10