Omar Dhanish
Omar Dhanish

Reputation: 875

Handling multiple data from Multiple Intent in single activity

I Am using two Activities (ListActivity and DatabaseBaseActivity) and directing both activities to single activity(DetailActivity) . I am using intent and putextra() on both activities to send array data to DetailActivity. The problem lies here , I dont know how to differentiate between different intents in single activity . And this is my ListActivity code

I think problem mainly lies in else if statement(I am getting nullpointerexception) in DetailActivity and If statement works fine

public class ListActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);


    final ArrayList<AppName> appNames = new ArrayList<AppName>();

    appNames.add(new AppName("app1","description1"));
    appNames.add(new AppName("app2","description2"));
    appNames.add(new AppName("app3","description3"));
    appNames.add(new AppName("app4","description4"));
    appNames.add(new AppName("app5","description5"));
    appNames.add(new AppName("app6","description6"));
    appNames.add(new AppName("app7","description7"));
    appNames.add(new AppName("app8","description8"));
    appNames.add(new AppName("app9","description9"));
    appNames.add(new AppName("app10","description10"));

    AppAdapter adapter = new AppAdapter(this , appNames);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                                       @Override
                                       public void onItemClick(AdapterView<?> parent, View view, int position, long id){

                                           String[] appdescription = getResources().getStringArray(R.array.appdescriptionname);
                                           final String appdescipl = appdescription[position];

                                           Intent intent1 = new Intent(getApplicationContext(), DetailActivity.class);
                                           intent1.putExtra("des","mydes");
                                           intent1.putExtra("appdescipl" , appdescipl);
                                           startActivity(intent1);



                                       }

   }

    );



}

this is my DatabaseBaseActivity code

public class DatabaseBaseActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);


    final ArrayList<AppName> appNames = new ArrayList<AppName>();

    appNames.add(new AppName("appdata1","description1"));
    appNames.add(new AppName("appdata2","description2"));
    appNames.add(new AppName("appdata3","description3"));
    appNames.add(new AppName("appdata4","description4"));
    appNames.add(new AppName("appdata5","description5"));
    appNames.add(new AppName("appdata6","description6"));
    appNames.add(new AppName("appdata7","description7"));
    appNames.add(new AppName("appdata8","description8"));
    appNames.add(new AppName("appdata9","description9"));
    appNames.add(new AppName("appdata10","description10"));

    AppAdapter adapter = new AppAdapter(this , appNames);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

                                            String[] appdatadescription = getResources().getStringArray(R.array.appdatabasedescriptionname);
                                            final String appdatadescipl = appdatadescription[position];

                                            Intent intent2 = new Intent(getApplicationContext(), DetailActivity.class);
                                            intent2.putExtra("data" , "mydata");
                                            intent2.putExtra("appdatadescipl" , appdatadescipl);

                                            startActivity(intent2);



                                        }
                                    }

    );



}

}

This is DetailActivity

public class DetailActivity extends Activity{

String description;

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

    TextView descriptionofapp = (TextView)findViewById(R.id.descriptionofapp);

    Bundle extras = getIntent().getExtras();

    if ( extras.getString("des").equals("mydes") && extras!=null){

        description = extras.getString("appdescipl");
        descriptionofapp.setText(description);
    }

    else if (extras.getString("data").equals("mydata") && extras!=null){
        description = extras.getString("appdatadescipl");
        descriptionofapp.setText(description);
    }


}

}

thanks in advance

Upvotes: 1

Views: 1095

Answers (2)

Exigente05
Exigente05

Reputation: 2211

try this,

Bundle extras = getIntent().getExtras();

if(extras.containsKey("des")){
    description = extras.getString("appdescipl");
    descriptionofapp.setText(description);
}
else if (extras.containsKey("data")){
    description = extras.getString("appdatadescipl");
    descriptionofapp.setText(description);
}

//...... conditions / check with unique key to differentiate from calling activity

Upvotes: 1

Rohan Bhatia
Rohan Bhatia

Reputation: 2010

you dont need to differentiate between intents. getIntent() simply returns the intent that started the activity. Try this-

String description =null;

description = getIntent().getStringExtra("appdescipl");

if(description==null) description = getIntent().getStringExtra("appdatadescipl");

descriptionofapp.setText(description);

Upvotes: 0

Related Questions