Doug McComber
Doug McComber

Reputation: 33

starting an activity from onCreate or onStart

I have an activity that is themed as Theme.Dialog. I want to have this activity started off the main activity during the latter's onCreate or onStart (depending on some conditions that are checked first). My problem is that when the "dialog" activity is started this way it doesn't display. I get a blank activity.

Manifest snippet:

 <activity android:name="CredentialsDialog"
        android:label="Credentials"
        android:theme="@android:style/Theme.Dialog"> 
 </activity>

Dialog layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="25dp"
  android:gravity="center_vertical|center_horizontal">

    <EditText android:id="@+id/txt_username"
        android:hint="Your username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
    <EditText android:id="@+id/txt_password"
        android:hint="Your password"
        android:password="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
</LinearLayout>

CredentialsDialog snippet:

public class CredentialsDialog extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.credentials_dialog);
        getWindow().setTitle("This is just a test");
        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);

        Bundle extras = getIntent().getExtras();
        String user = extras.getString("user");
        String pass = extras.getString("pass");

        // get handle on EditText fields
        final EditText textUser = (EditText) findViewById(R.id.txt_username);
        final EditText textPass = (EditText) findViewById(R.id.txt_password);

        // pre-populate dialog username EditText with username if it exists
        if (user != "") {
            textUser.setText(user);
        }

        // pre-populate dialog password EditText with password if it exists
        if (pass != "") {
            textPass.setText(pass);
        }
    }
}

MainActivity snippet:

(in either onCreate or onStart)
    if (!loginData()) {
        loginDialog();
    } else {    
        bLoggedIn = qrzLogin();
        toggleUi();
    }
(snip...)
    public void loginDialog() {
        Intent i = new Intent(this, CredentialsDialog.class);
        i.putExtra("user", MainActivity.user);
        i.putExtra("pass", MainActivity.pass);
        startActivity(i);
    }

In MainActivity, if I called loginDialog() a second time immediately after the first, the second would work while the first would be a blank activity. What am I doing wrong? Opinions welcome too. :)

Thanks, Doug

Upvotes: 0

Views: 1271

Answers (1)

Doug McComber
Doug McComber

Reputation: 33

Switching to overriding onCreateDialog and onPrepareDialog allowed me to add a TextWatcher to the EditText fields in my AlertDialog. This was the end goal anyway. Also, I have to say I like the dialog handling via onCreateDialog/onPrepareDialog. You can teach an old dog new tricks afterall!

Upvotes: 1

Related Questions