按Back Button时,发送前一个Activity的值

时间:2016-08-13 13:05:58

标签: android

大家好我想要这个:按动作栏后退按钮,发送前一个活动的值。并检查价值是否留在那里。

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; go home
                //i'm giving error because i mustnt create new activity i must to send previous activity..
                LoginActivity yeni=new LoginActivity();
                yeni.setPassword("");
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

当我按下后退按钮时,退出应用程序并进入登录屏幕,但我必须更改loginactivity中的值,因为如果我再次登录不活动,请登录相同的值并启动主要活动......

显然,当我按下后退按钮时,我必须向LoginActivity中的方法发送一个可能为null的值。像这样:

   public void setPassword(String comingpass)
    {
        _passwordText.setText(comingpass);

    }

当它知道_passwordtext=null留在那里并等待新登录时......

3 个答案:

答案 0 :(得分:2)

使用startActivityForResult()Getting a Result from an Activity | Android Developers

答案 1 :(得分:1)

hashtag_list = params[:message][:hashtag_primary]
@hashtags = Hashtag.where({:name => hashtag_list}).first_or_create do |hashtag|
   hashtag.creator = current_user.id
end

答案 2 :(得分:0)

// call activity with startActivityForResult
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);


//back press
switch (item.getItemId()) {
        case android.R.id.home:
 Intent returnIntent = new Intent();
returnIntent.putExtra("flag",1);
setResult(Activity.RESULT_OK,returnIntent);
finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }


//handle back in main activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
        if(data.getIntExtra("flag",0)==1){
        yeni.setPassword("");
    }
    }
    if (resultCode == Activity.RESULT_CANCELED) {
        //Write your code if there's no result
    }
}
}