简体   繁体   English

android在活动/布局之间切换

[英]android switch between activities/layouts

I would like to show loadingActivity while "activity1" is executing some code (working), after that, show again activity1. 我想显示loadingActivity而“activity1”正在执行一些代码(工作),之后再显示activity1。 However, if I do not want to start activity1 again, only switch its layouts when doSomeStuff ends. 但是,如果我不想再次启动activity1,只在doSomeStuff结束时切换其布局。 Thank you. 谢谢。

activity1 活动1

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent myIntent = new Intent(getApplicationContext(), loadingActivity.class);
        startActivityForResult(myIntent, 0);

        //Do some stuff while loadingActivity is showed

        doSomeStuff()

        //here I want to show again this activity and hide loading one

loadingActivity loadingActivity

public class loadingActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(R.layout.loading);

    }

For this you should use a ProgressDialog , this will allow you to easily show a loading indicator and once the work is done you can easily remove it. 为此,您应该使用ProgressDialog ,这将允许您轻松显示加载指示器,一旦完成工作,您可以轻松地将其删除。 The code below should work of you. 下面的代码应该对你有用。

Show Dialog: 显示对话框:

    ProgressDialog dialog = new ProgressDialog(YourActivityClass.this);
    dialog.setMessage("Loading Activity...");
    dialog.show();

    //Do your long running work here

Dismiss dialog: 关闭对话框:

    dialog.dismiss();

You can set the dialog as a class level variable if you want to show and dismiss it in different methods. 如果要以不同方法显示和关闭对话框,可以将对话框设置为类级变量。

Also from looking at your code you might be blocking the activity from ever loading if your long running work is not happening on a background thread. 另外,如果您在后台线程上没有进行长时间运行的工作,那么从查看代码开始,您可能会阻止活动加载。 You cannot do long running work inside of onCreate without offloading the work to a background thread. 如果不将工作卸载到后台线程,则无法在onCreate内部进行长时间的运行。 For easy threading in Android you should use the AsyncTask class. 为了在Android中轻松进行线程化,您应该使用AsyncTask类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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