简体   繁体   中英

Where to put activity code?

so I have a code that checks something and I put it in the onCreate() of an Activity. I want to know if it's correct to put it there and also, for some reason the code that checks the Main Activity doesn't work at all, the second one which has a toast works. I think the problem may be in an AlertDialog. Here's the one with the toast:

AlertDialog.Builder Dial = new AlertDialog.Builder(Screen.this);
Dial.setTitle(R.string.Dial_Tit);
Dial.setMessage(R.string.Dial_Mes);
Dial.setPositiveButton("OK", PosBC());
Dial.setNegativeButton(R.string.Dial_NegBC, NegBC());
Dial.show();

Note: both buttons have methods, I just didn't post them. The problem is that the alert doesn't even show. And also for some reason the toast does work, it like automatically clicks thebutton, even thought the method has an intent which doesn't work.

More code as requested:

    private DialogInterface.OnClickListener NegBC() {
    Intent moveToStart;
    moveToStart = new Intent(Screen.this, Launch.class);
    startActivity(moveToStart);
    return null;
}

private DialogInterface.OnClickListener PosBC() {
    startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    Toast.makeText(getApplicationContext(), R.string.settingsToast, Toast.LENGTH_LONG).show();
    return null;
}

Update : I've added the create() method which shows the dialog but it goes like this : when activity is created shows toast, press back goes to settings, press back from settings shows dialog, buttons don't work.

Write your AlertDialog code in OnCreateDialog and Start a in OnCreate AsynTask for Checking purpose and once your task is finished, inside onPostExecute close the Dialog.

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

    showDialog(0x01);

}

@Override
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder Dial;
    switch (id) {

        case 0x01:
            Dial = new AlertDialog.Builder(Screen.this);
            Dial.setTitle(R.string.Dial_Tit);
            Dial.setMessage(R.string.Dial_Mes);
            Dial.setPositiveButton("OK", PosBC());
            Dial.setNegativeButton(R.string.Dial_NegBC, NegBC());
            Dial.create();
        break;

    default:
        break;
    }

    return super.onCreateDialog(id);
}

onCreate() will be called whenever you start the application and if it is not cached in the device's RAM.

I do not understand what you are trying to achieve other than that, please edit your post by adding more of your code and I will also edit my answer to be more detailed.

use this code to display alertDialog in android on clicking a button:

     package .....; // name of your package.

        import android.app.Activity;
        import android.app.AlertDialog;
        import android.content.DialogInterface;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.Toast;


public class AlertDialogActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button btnAlertTwoBtns = (Button) findViewById(R.id.button1);

        btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                // Creating alert Dialog with two Buttons

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("    "); //type your title here insid the quotes.

                // Setting Dialog Message
                alertDialog.setMessage(" "); //type the message which is to be displayed

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.ic_launcher); // set the icon from drawable folder just put the icon file in drawable folder.

                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); // just a sample code to tell that what things you can do here
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();

            }
        });
}
}

Ok, I solved it myself, turns out it was just logic missing :D. Sorry!

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