如何在下载/可用数据之前保留活动

时间:2013-05-29 10:35:16

标签: android

我对android编程很新,想澄清一个场景。好的,我有两项活动叫A& B.当活动A启动时,它要求在后台下载一些数据,并且在3秒后启动活动B.活动A就像一个闪屏。因此,当B活动启动时,我想保持并等待数据完全加载,以便我可以在活动B中初始化下载的数据。此时活动B甚至在下载数据之前启动。如果是这种情况,在加载B然后下载数据之后,我将如何动态加载活动B中的数据? 另外,有没有办法在活动B中保存onCreate,直到下载数据?

提前谢谢。

3 个答案:

答案 0 :(得分:1)

我认为您正在使用AsyncTask进行后台数据下载。如果是,则只需从asyncTask的onPostExecute()方法启动活动B.

你可以创建seprate Thread或innerclass

    public class ThreadCutFiles extends AsyncTask<Object, Object, Boolean>{

    private ProgressDialog progressDialog;
    private Context context;
    private Handler handler;

    public ThreadCutFiles( Context context, Handler handler ) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("File Manager");
        progressDialog.setMessage("Copying Files...");      
        progressDialog.show();
    }

    @Override
    protected Boolean doInBackground(Object... params) {
        //ur downloading code
        return true;

    }   

    @Override
    protected void onPostExecute(Boolean result) {
        progressDialog.dismiss();

        }
    }
}

答案 1 :(得分:1)

假设您正在使用AsyncTask,您可以在活动B中创建一个回调,您可以在AsyncTask的onPostExecute中调用,将下载的数据传递给活动B. 这样,您只需要在活动B onCreate中放置一个加载对话框,并负责在回调中加载数据。

A:

public class ActivityA extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);
    //start download task
    ActivityB b = new ActivityB();
    startActivity(new Intent(getApplicationContext(), b.getClass()));
    new Task(b.new DownloadCallback()).execute();
   }

 }

B:

public class ActivityB extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
}

@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    //show a progress dialog
    return super.onCreateView(name, context, attrs);
}

private void loadData(/*Your data*/){
    //Load your data
    System.out.println("data loaded");
}

public class DownloadCallback {

    public void onFinish(/*Your data*/) {
        loadData(/*Your data*/);
    }

   }

}

任务:

public class Task extends AsyncTask<Void, Void, Object>{//Change the Object to whatever your data is

private DownloadCallback callback;

public Task(DownloadCallback callback) {
    this.callback = callback;
}

@Override
protected Object doInBackground(Void... params) {
    //Download data here
    //Change the Object return value to whatever your data is
    return true;
}   

@Override
protected void onPostExecute(Object result) {
    //Change the Object to whatever your data is and pass to the finish method
    callback.onFinish(/*Your data*/);
  }
 }

答案 2 :(得分:1)

您可以实现Loader(首选)或Async Task来加载Activity A中的数据。当数据加载完成后,在onLoadFinished或onPostExecute中打开活动B并传入带有intent的下载数据。我建议在活动A中显示加载图标,以便用户知道您的应用正在后台进行一些工作。