0

I am trying to work out the best approach to run a background task while showing a spinner dialog box and handle changes such as rotation of the device or if the device goes into a screen lock and then unlock state.

Currently my app has to work with API 8 up to 16 so I know that the getLastNonConfigurationInstance() is now redundant from api 11 (I think) up and I tried this method to make sure that it would not work on an API 8 device which it didn't. I then read about asynctaskloader but this is not available for API 8.

Finally I read about intentservice which appears could be the answer but I just need to know how to handle this with config changes where the activity is restarted.

My background tasks are either to loop through a sqlite database and export the rows as a CSV or to import from a CSV. These I have done already through an AsyncTask but if I rotate the device or lock the screen , they crash.

As the Activity is restarted I suspect that if I used a intentservice it would need to know the new activity "reference" so to speak. With an AsyncTask my understanding is that it is attached to the activity that started it so when it is re-created it looses that link.

EDIT**

Since my original question Marcin has helped out with some of the issues I have had. However it appears that the link to an example of an activity to a fragment does not match up to the source code that can be downloaded and so my question is as follows now :

I am trying to work out how to implement a fragment within my listactivity. I know that there is a Listfragment but if I change my listactivity to this, I will not be able to start the listfragment from an activity as I have tried this already and reading posts here this cannot be done unless the listfragment is inside an activity or fragment activity.

My reason for a fragment is to control configuration changes and the listactivity is used to display a list of files and folders , then the user can select the file they want to use , click import and then I need to start the fragment which will have the asynctask to import the data.

I have yet to find an example of a listactivity with a fragment that shows this and I am trying to figure this out.

Please ket me know if you require any code from my project

Thanks

TimCS

4

1 回答 1

0

看看setRerainInstance()。您可以将 AsyncTask 放入其中,并且在配置更改后不会重新创建它。您也可以阅读这篇文章,它会有所帮助

package com.example.we.helpers;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.example.we.MainActivity;

public class ImageFragment extends Fragment{
    public static final String URL = "http://link.com";
    private ImageTask task;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    public void startDownloading(){
        task = new ImageTask();
        task.execute();
    }

    public class ImageTask extends AsyncTask<Void, Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(Void... arg0) {
            Bitmap bmp =null;
            try{
                URL ulr = new URL(URL);
                HttpURLConnection con = (HttpURLConnection)ulr.openConnection();
                InputStream is = con.getInputStream();
                bmp = BitmapFactory.decodeStream(is);
            }catch(Exception e){
                    e.printStackTrace();
            }
            return bmp;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            if(getActivity() instanceof MainActivity == false){
                return;
            }

            MainActivity activity = (MainActivity) getActivity();
            activity.onImageLoaded(result);
        }


    }
}

这就是我运行片段的方式:

ImageFragment fragment = new ImageFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(fragment, LOADER);
transaction.commit();
fragment.startDownloading();

以下是来自 MainActivity 的导入:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
于 2013-08-06T16:25:35.260 回答