简体   繁体   中英

Passing a dialog from one activity to another

I am trying to create a simple loading screen, i dont need any progress bar or anything just an image.

What i need:-

-Start activity A

-Click button for Activity B

-Clicking button also starts a dialog that just displays my image while activity B is loading

-Once activity B is loaded i would like to keep the dialog displaying until my renderer has finished loading all textures.

Is it possible to pass an active dialog from one activity to another?

    //Activity A
    StartButton.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View arg0) 
        {
            Intent intent = new Intent(context, ActivityB.class);               
            Dialog loader_dialog = new Dialog(context,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
            loader_dialog.setContentView(R.layout.loading_screen);
            loader_dialog.show();
            //this obviously doesnt work but is there something similar i can do?
            //intent.putExtra("LoadingScreen", loader_dialog); 
            startActivity(intent); 
            finish(); 
        } 
    }); 

if this is not possible is there some other way I can get the desired effect?

You can create the dialog box and let it appear in onCreate() or onStart() method of your next activity - B activity:

public class ActivityB extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Dialog loader_dialog = new Dialog(context,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        loader_dialog.setContentView(R.layout.loading_screen);
        loader_dialog.show();  

        ...
    }

}

I think that can perform the task you want.

If you really want the dialog to be shown during the transition to the second Activity, you can use one single Activity instead of two and change the layout of that Activity to use the layout of the second Activity instead of calling another Activity. However, this can be complicated since you will implement the features of your two Activities into a single one.


EDIT:

You can use fragments instead of Activities.

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