back to previous activity with intent

2019-01-09 13:38发布

I have two activity, and when I press enter on first activity, it will open second activity, it contains a ListView and when I choose an item from ListView, it will get its value and bring back to first activity

this is what I've tried;

on second activity

listPerasat.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            NamaPrst = ((TextView) view.findViewById(R.id.txtListNamaPrst)).getText().toString();
            Intent i = new Intent();
            i.putExtra("NAMA_PERASAT", NamaPrst);
            finish();
        }
    });
}

on first activity

edtText.setText(getIntent().getStringExtra("NAMA_PERASAT")); // inside onCreate

or

public void onActivityResult(int requestCode,int resultCode, Intent data)
{
    edtText.setText(getIntent().getStringExtra("NAMA_PERASAT"));
}

but nothing happen. how can I get intent and back to previous activity?

6条回答
啃猪蹄的小仙女
2楼-- · 2019-01-09 14:20

You forgot to start an activity so right before

finish();

i.e.

startActivity(intent);
// or to start with a back result
// startActivityForResult(intent, YOUR_REQUEST_CODE, EXTRA_BUNDLE);
finish();
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-09 14:20
ら.Afraid
4楼-- · 2019-01-09 14:21

In second activity edit your code like this

Intent i = new Intent();
        i.putExtra("NAMA_PERASAT", NamaPrst);
        setResult(RESULT_OK, i);
        finish();

then in first activity's onActivityResult(), first check if the result was "OK" i.e. something was selected.

   public void onActivityResult(int requestCode,int resultCode, Intent data)
    {
      if(resultCode == Activity.RESULT_OK){
        edtText.setText(data.getStringExtra("NAMA_PERASAT"));
      }
    }

If after opening second activity you didn't selected anything and pressed back button then you will get errors since nothing was passed from second activity. So first check that if the setResult(RESULT_OK, i); was executed i.e. extras were added and we manually set that everything is OK now.

查看更多
一夜七次
5楼-- · 2019-01-09 14:22

Try this:

Intent resultIntent = new Intent(ctx, ViewMessageDialog.class);
resultIntent.putExtra("NAMA_PERASAT", message);
ctx.startActivity(resultIntent);
查看更多
贼婆χ
6楼-- · 2019-01-09 14:23

call the second activity for result. it will return your selected data on OnAcitiviyResult. just like below:

link to post

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-09 14:31

You want to call startActivityForResult() for this. So in your first Activity something like

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(i, SOME_REQUEST_CODE); // you give SomeRequesSOME_REQUEST_CODE an int value

then in your onItemClick() you need to call setResult() and send back the Intent. This will call onActivityResult() in your first Activity

listPerasat.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        NamaPrst = ((TextView) view.findViewById(R.id.txtListNamaPrst)).getText().toString();
        Intent i = new Intent();
        i.putExtra("NAMA_PERASAT", NamaPrst);
        setResult(RESULT_OK, i);
        finish();
    }
});
}

In onActivityResult() in your first Activity, don't call getIntent(). This will try to use the Intent that originally started your first Activity. Instead, use the Intent you passed back

@Override
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
    edtText.setText(data.getStringExtra("NAMA_PERASAT"));
}

See this answer for another example

查看更多
登录 后发表回答