when mesg box exits it returns to prevoius activyty

I got code from stack overflow to bring up a mesg box.

When I call up a mesga box in activty ActGetKey, it goes back to the starting acticty MainActivity, after the exit button is pressed.

mesg box code:

public void msbox(String str,String str2) { AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this); dlgAlert.setTitle(str); dlgAlert.setMessage(str2); dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { finish(); } }); dlgAlert.setCancelable(true); dlgAlert.create().show(); }

code for MainActivity public class MainActivity extends AppCompatActivity {

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

    keyDB db;
    db = new keyDB(this);
    db.setUp();

    cPassKey mPassKey = new cPassKey("mysec","mypublic","myphase");
    db.updateExchange("ted2", mPassKey);

    Cursor cursor =db.selectKey("ted2");
    // get keys in current pos of database
    cPassKey mret=db.loadRecord(cursor);

    String name;
    Cursor test =db.selectKey("ted1");
    name = test.getString(2);
    name = test.getString(3);

    cursor = db.selectRecords();

    int id; String ted;
    if (cursor != null)
    {
        do {
            id = cursor.getInt(0);
            ted = cursor.getString(1);
        } while (cursor.moveToNext());
    cursor.close();
}

 }




public void sendMessage(View view) {

// Intent intent = new Intent(this, ActGetKey.class); Intent intent = new Intent(this, ActGetKey.class); startActivity(intent); }

}

code for ActGetKey Activity public class ActGetKey extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

String selItem=null;
long d=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act_get_key);

    keyDB db;
    db= new keyDB(this);
    Cursor cursor=db.selectRecords();
    String ted="na";
    int id=0;
    if (cursor != null) {

        // get keys in current pos of database
        cPassKey mPassKey=db.loadRecord(cursor);
        // displkay the keys on app
        loadKeys(mPassKey);
        // fill up spinner
        Spinner spinner = (Spinner) findViewById(R.id.spinner2);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();

        if (cursor != null) {
            do {
                id = cursor.getInt(0);
                ted = cursor.getString(1);
                categories.add(ted);
            } while (cursor.moveToNext());
            cursor.close();
        }

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);

        // set selected item
        spinner.setSelection(0);
    }

}

public void Update3(View view)

{ d++; EditText temp;

    temp = findViewById(R.id.publicKey);
    String strPublicKey=(String)temp.getText().toString();
    if (isValidString(strPublicKey))
    {
        msbox( getString(R.string.badtext), "Text for publick key is invalid");
        return;
    }

    temp = findViewById(R.id.publicKey);
    String strPrivetKey=(String)temp.getText().toString();

    temp = findViewById(R.id.publicKey);
    String strKeyPhrase=(String)temp.getText().toString();

    // check for bad dada

}

boolean isValidString( String test)
{
    if (test==null)
        return false;

    return true;
}


public void Update2(View view) {
    // put keys in keyclass

    msbox("kkk","kjjh");
    if (true)
        return;

    keyDB db;
    db= new keyDB(this);
    cPassKey mPassKey = getKeys();
    db.updateExchange(selItem, mPassKey);
    // pus keyclass in data base

}



@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    selItem = parent.getItemAtPosition(position).toString();
    keyDB db;
    db= new keyDB(this);
    Cursor cursor =db.selectKey(selItem);
    // get keys in current pos of database
    cPassKey mPassKey=db.loadRecord(cursor);
    // displkay the keys on app
    loadKeys(mPassKey);

}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}

boolean loadKeys(cPassKey mPassKey)
{
    EditText temp;

    temp =  findViewById(R.id.publicKey);
    temp.setText(mPassKey.publickKey);
    temp =  findViewById(R.id.secretKey);
    temp.setText(mPassKey.secretKey);
    temp =  findViewById(R.id.phraseKey);
    temp.setText(mPassKey.phraseKey);

    return true;
}


public void msbox(String str,String str2)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    dlgAlert.setTitle(str);
    dlgAlert.setMessage(str2);
    dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            finish();
        }
    });
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();
}


cPassKey getKeys()
{
    EditText temp;

    temp =  findViewById(R.id.publicKey);
    String strPublicKey= temp.getText().toString();
    temp =  findViewById(R.id.secretKey);
    String StrSecretKey= temp.getText().toString();
    temp =  findViewById(R.id.phraseKey);
    String StrPhraseKey= temp.getText().toString();

    cPassKey mPassKey = new
            cPassKey( StrSecretKey,strPublicKey, StrPhraseKey);
    return mPassKey;
}

}

public class ActGetKey extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

String selItem=null;
long d=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act_get_key);

    keyDB db;
    db= new keyDB(this);
    Cursor cursor=db.selectRecords();
    String ted="na";
    int id=0;
    if (cursor != null) {

        // get keys in current pos of database
        cPassKey mPassKey=db.loadRecord(cursor);
        // displkay the keys on app
        loadKeys(mPassKey);
        // fill up spinner
        Spinner spinner = (Spinner) findViewById(R.id.spinner2);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();

        if (cursor != null) {
            do {
                id = cursor.getInt(0);
                ted = cursor.getString(1);
                categories.add(ted);
            } while (cursor.moveToNext());
            cursor.close();
        }

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);

        // set selected item
        spinner.setSelection(0);
    }

}

public void Update3(View view)

{ d++; EditText temp;

    temp = findViewById(R.id.publicKey);
    String strPublicKey=(String)temp.getText().toString();
    if (isValidString(strPublicKey))
    {
        msbox( getString(R.string.badtext), "Text for publick key is invalid");
        return;
    }

    temp = findViewById(R.id.publicKey);
    String strPrivetKey=(String)temp.getText().toString();

    temp = findViewById(R.id.publicKey);
    String strKeyPhrase=(String)temp.getText().toString();

    // check for bad dada

}

boolean isValidString( String test)
{
    if (test==null)
        return false;

    return true;
}


public void Update2(View view) {
    // put keys in keyclass

    msbox("kkk","kjjh");
    if (true)
        return;

    keyDB db;
    db= new keyDB(this);
    cPassKey mPassKey = getKeys();
    db.updateExchange(selItem, mPassKey);
    // pus keyclass in data base

}



@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    selItem = parent.getItemAtPosition(position).toString();
    keyDB db;
    db= new keyDB(this);
    Cursor cursor =db.selectKey(selItem);
    // get keys in current pos of database
    cPassKey mPassKey=db.loadRecord(cursor);
    // displkay the keys on app
    loadKeys(mPassKey);

}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}

boolean loadKeys(cPassKey mPassKey)
{
    EditText temp;

    temp =  findViewById(R.id.publicKey);
    temp.setText(mPassKey.publickKey);
    temp =  findViewById(R.id.secretKey);
    temp.setText(mPassKey.secretKey);
    temp =  findViewById(R.id.phraseKey);
    temp.setText(mPassKey.phraseKey);

    return true;
}


public void msbox(String str,String str2)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    dlgAlert.setTitle(str);
    dlgAlert.setMessage(str2);
    dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            finish();
        }
    });
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();
}


cPassKey getKeys()
{
    EditText temp;

    temp =  findViewById(R.id.publicKey);
    String strPublicKey= temp.getText().toString();
    temp =  findViewById(R.id.secretKey);
    String StrSecretKey= temp.getText().toString();
    temp =  findViewById(R.id.phraseKey);
    String StrPhraseKey= temp.getText().toString();

    cPassKey mPassKey = new
            cPassKey( StrSecretKey,strPublicKey, StrPhraseKey);
    return mPassKey;
}

}


That is because of the finish() method inside the onClickListener . Finish() doesn't cancel the dialog, it finishes the activity. Use dismiss() if you are trying to hide the dialog

链接地址: http://www.djcxy.com/p/67912.html

上一篇: 时间确定SCons目标

下一篇: 当mesg框退出时,它返回到prevoius激活状态