Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling one Activity from another in Android

Tags:

android

How can I call another activity from one (the current) activity?

And for the same I want to call an activity which contains a dialog message box from my current activity.

like image 411
OnkarDhane Avatar asked Mar 01 '11 09:03

OnkarDhane


People also ask

How do you call an activity from another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

How can we redirect from one activity to another?

You need to use Intent Class in order to redirect from one activity class to another activity class. onOptionsItemSelected() Method will be trigged when you click the 'show setting' menu in the action bar.


2 Answers

First question:

Use the Intent to call another Activity. In the Manifest, you should add

<activity android:name="ListViewImage"></activity> <activity android:name="com.company.listview.ListViewImage"> </activity> 

And in your current activity,

btListe = (ImageButton)findViewById(R.id.Button_Liste);     btListe.setOnClickListener(new OnClickListener()     {    public void onClick(View v)         {             intent = new Intent(main.this, ListViewImage.class);             startActivity(intent);             finish();         }     }); 

Second question:

sendButton.setOnClickListener(new OnClickListener() {         public void onClick(View v) {             String valueString = editValue.getText().toString();             long value;             if (valueString != null) {                 value = Long.parseLong(valueString);             }             else {                 value = 0;             }              Bundle sendBundle = new Bundle();             sendBundle.putLong("value", value);              Intent i = new Intent(Activity1.this, Activity2.class);             i.putExtras(sendBundle);             startActivity(i);              finish();         }     }); 

and in Activity2:

 Bundle receiveBundle = this.getIntent().getExtras();     final long receiveValue = receiveBundle.getLong("value");     receiveValueEdit.setText(String.valueOf(receiveValue));     callReceiverButton.setOnClickListener(new OnClickListener() {         public void onClick(View v) {             Intent i = new Intent(Activity2.this, Receiver.class);             i.putExtra("new value", receiveValue - 10);         }     }); 
like image 95
LTEHUB Avatar answered Oct 02 '22 09:10

LTEHUB


check the following code to call one activity from another.

Intent intent = new Intent(CurrentActivity.this, OtherActivity.class); CurrentActivity.this.startActivity(intent); 
like image 27
Tushar Vengurlekar Avatar answered Oct 02 '22 09:10

Tushar Vengurlekar