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.
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.
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);
}
});
check the following code to call one activity from another.
Intent intent = new Intent(CurrentActivity.this, OtherActivity.class);
CurrentActivity.this.startActivity(intent);
I used following code on my sample application to start new activity.
Button next = (Button) findViewById(R.id.TEST);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent( view.getContext(), MyActivity.class);
startActivityForResult(myIntent, 0);
}
});
As we don't know what are the names of your activities classes, let's call your current activity Activity1, and the one you wish to open - Activity2 (these are the names of the classes)
First you need to define an intent that will be sent to Activity2:
Intent launchActivity2 = new Intent(Activity1.this, Activity2.class);
Then, you can simply launch the activity by running:
startActivity(launchActivity2 );
Add the following to your on button click event:
Intent intent = new Intent(First.this, Second.class);
startActivity(intent);
finish();
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent)
which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent)
-- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener()
you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move (It may contain anything like you are saying dialog box).
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener()
at any place where you want to switch between your activities.
Put this inside the onCreate() method of MainActivity1.java
Button btnEins = (Button) findViewById(R.id.save);
btnEins.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intencion = new Intent(v.getContext(),MainActivity2.class );
startActivity(intencion);
}
});
I have implemented this way and it works.It is much easier than all that is reported.
We have two activities : one is the main and another is the secondary.
In secondary activity, which is where we want to end the main activity , define the following variable:
public static Activity ACTIVIDAD;
And then the following method:
public static void enlaceActividadPrincipal(Activity actividad)
{
tuActividad.ACTIVIDAD=actividad;
}
Then, in your main activity from the onCreate
method , you make the call:
actividadSecundaria.enlaceActividadPrincipal(this);
Now, you're in control. Now, from your secondary activity, you can complete the main activity. Finish calling the function, like this:
ACTIVIDAD.finish();
The following code demonstrates how you can start another activity via an intent.
Start the activity with an intent connected to the specified class
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
Activities which are started by other Android activities are called sub-activities. This wording makes it easier to describe which activity is meant.
check the following code to open new activit.
Intent f = new Intent(MainActivity.this, SecondActivity.class);
startActivity(f);
Very easiest way to call one activity to another is
startActivity( new Intent( getApplicationContext(), YourActivity.class ) );
Create view like on which you have to add click.
Add onclick to it , you can do it via xml
file or in your java class(for example if its button you can directly add
onClick properties in xml)
In your class file init that view like : Button buttonname;
buttonname=findViewbyId
then buttonname.setOnClickListner()
pass newOnClickListner
(casting the widget. Note:no need of this step if
you have use onclick in the xml properties)
startActivity( new Intent( getApplicationContext(), ToOtherActivity.class ) );
or
Intent intent = new Intent(this, ToOtherActivity.class);
startActivity(intent);