Medlin Moni Medlin Moni - 5 months ago 17
Android Question

How To differentiate two different Intent to same activity?

Am new for android development i need to differentiate intent two different intent to same activity let me explain clearly i have one button when user press that button it will trigger to other activity in which it will carry some intent value from there based user will enter value based on that i will populate listview from where i trigger the intent from intent suppose if user press the listview from there how to pass the intent with some additional intent values and how to differentiate two intent in receiving activity here is wh

at i have tried so far:




@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.add:
int id=timertrackerdao.getNotesCount();
if(id>0){
Toast.makeText(getContext().getApplicationContext()," Please Finish The Pending Visit",Toast.LENGTH_SHORT).show();
}
else {
Intent intent = new Intent(getActivity().getApplicationContext(), AddNotes.class);
intent.putExtra("taskid",taskid);
intent.addFlags(0);
startActivityForResult(intent, 1);
}}
return super.onOptionsItemSelected(item);
}


This is where i will trigger intent from menu bar

Intent intent = new Intent(getActivity().getApplicationContext(), AddNotes.class);
Bundle bundle=new Bundle();
bundle.putInt("addnoteid",id);
bundle.putInt("taskid",taskid);
bundle.putString("checkin",checkin);
intent.addFlags(1);
startActivityForResult(intent, 1);


This is where i will select the value from listview this is another set of values. Here in the receiving activity how am trying to differentiate intent:

Intent intent=getIntent();
if(intent.getFlags()==0) {
if (intent != null) {
taskid = intent.getIntExtra("taskid", 0);
}
}
else {
Bundle extras = getIntent().getExtras();
if (extras != null) {
check.setEnabled(false);
// TaskID=extrass.getInt("noteid");
id = extras.getInt("addnoteid");
date = extras.getString("checkin");
TaskID = extras.getInt("taskid");
createNotification(date);
AddNote(id);
}
}


Now my question is how to differentiate two intent one from button click and another by clicking listview and how to differentiate that in receiving activity??

Answer

Put an string with both the intent like this

intent.putExtra("from","button");

And

intent.putExtra("from","listview");

Now in another activity

    if(getIntent().getStringExtra("from").equals("button"))
{  //do here
}else if(getIntent().getStringExtra("from").equals("listview"))
{   // do here
}