开发者

Android Intent with explicit handler and id

开发者 https://www.devze.com 2023-03-31 14:37 出处:网络
I have a ListActivity which should call an edit form, much like in the Notepad 开发者_StackOverflow社区tutorial. The difference is, that I don\'t want to call an ACTION_EDIT intent, but the EditorActi

I have a ListActivity which should call an edit form, much like in the Notepad 开发者_StackOverflow社区tutorial. The difference is, that I don't want to call an ACTION_EDIT intent, but the EditorActivity-class directly and also send it the ID. Right now I only can figure out how to directly call the intent

startActivity(new Intent(Intent.ACTION_EDIT,ContentUris.withAppendedId(getIntent().getData(), id)));

or to send an ID with a general request for edit

startActivity(new Intent(getBaseContext(),LocationEditorActivity.class));

So how do I match them together now?


Doing this...

startActivity(new Intent(Intent.ACTION_EDIT,ContentUris.withAppendedId(getIntent().getData(), id)));

... is like asking the OS: "hey, I want to you to open an activity that handles this kind of URI and supports the Intent.ACTION_EDIT action".

Then, the choosen activity has to look inside the Uri and extract the ID and process it. So, if you want to launch the activity directly, you just have to explicitly send the ID to that activity:

Intent intent = new Intent(getBaseContext(),LocationEditorActivity.class);
intent.putExtra("the_id", id);
startActivity(intent);

Then, inside the activity, instead of looking for the ID inside the Uri data, you get it from the extras:

long theId = getIntent().getExtras().getLong("the_id", -1);


? try to build intent like this:

Intent intent = new Intent(getApplicationContext(), LocationEditor.class);
intent.putExtra("Id", id);
startActivity(intent);

in the recieving Activity:

Intent intent = getIntent();
int id = intent.getExtras().getInt("Id");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号