Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What i am doing is sending Intent to MainActivity from Listview the listview item i am displaying in alertbox

Java
holder.txtStore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String value = holder.txtStore.getText().toString();
            Intent myIntent = new Intent(view.getContext(),MainActivity.class);
            myIntent.putExtra("restaurant_name", value);
            try {
                context.startActivity(myIntent);
            } catch (android.content.ActivityNotFoundException ex) {
                ex.printStackTrace();
                Toast.makeText(context, "yourActivity is not founded", Toast.LENGTH_SHORT).show();
            }
        }
    });


In MainActivity right now i am using button to start getIntent but i dont want to use button i just want to start getIntent function when alertbox disappear or ya when MainActivity start

This is my code to call getIntent using button.

Java
mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = getIntent();
            String restaurant_name = intent.getStringExtra("restaurant_name");
            Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
        if(restaurant_name != null ) {
            if (restaurant_name.equals("Romys")) {
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(26.89553, 75.82842))
                        .title("ROMYS"))
                        .showInfoWindow();


            }
        }else {
                Toast.makeText(MainActivity.this,"It was not", Toast.LENGTH_LONG).show();
            }
        }
    });


What I have tried:

i use onResume onRestart but not work for me
Posted
Updated 2-Apr-16 6:33am

1 solution

Then add the code at the onCreate() function. Just the way you have written the code in the event handler, write the code in the activity's event handler; for creation.
Java
// Function executed when activity starts.
public void onCreate(Bundle b) { 
   Intent intent = getIntent();
   // ... Following code
}

This way, you will get your intent captured. Basically, to capture the Intent object anywhere, just do the getIntent() function in instance contexts.
 
Share this answer
 
Comments
C0DE_007 2-Apr-16 15:17pm    
Sir as u said i put getIntent() in onCreate but app is crashing
C0DE_007 2-Apr-16 15:27pm    
i also try to use onNewIntent but after click app crash

protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
getIntent();
String restaurant_name = intent.getStringExtra("restaurant_name");
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
if(restaurant_name != null ) {
if (restaurant_name.equals("Romys")) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f)); // App crash show error here

}else {
Toast.makeText(MainActivity.this,"It was not", Toast.LENGTH_LONG).show();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900