Using Intent in an Android Application to Show Another Activity

Using Intent in an Android application to show another activity

The issue was the OrderScreen Activity wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.

<activity android:name=".OrderScreen" />

How do I pass data between Activities in Android application?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

The docs for Intents has more information (look at the section titled "Extras").

How to return results from one activity to another using an Intent in Android?

You should put anything you need in the result intent and then get them from extras.

just do it like this :

in Splash:

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent();
data.putExtra("n1", display.getText().toString());
dat.putExtra("n2", disp.getText().toString());
setResult(RESULT_OK, dat);

finish();

}
});

and in MainActivity:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == code) {
if (resultCode == RESULT_OK) {

// Toast.makeText(this, data.getData().toString(),
//Toast.LENGTH_SHORT).show();
a = data.getStringExtra("n1");

num = Integer.parseInt(a);
t.setText("" + num);
c = data.StringExtra("n2");
num1 = Integer.parseInt(c);
t.setText("" + num1);
int total = num1 + num;

}
}
}

How to start new activity on button click

Easy.

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);

Extras are retrieved on the other side via:

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("key"); //if it's a string you stored.
}

Don't forget to add your new activity in the AndroidManifest.xml:

<activity android:label="@string/app_name" android:name="NextActivity"/>

Calling one Activity from another in Android

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);
}
});

Moving from one activity to another Activity in Android

You haven't defined NextActivity in the AndroidManifest.xml file.

Add these lines in android manifest after</activity> tag. It should work.

<activity
android:name=".NextActivity" >
</activity>

final code will be

<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="Main Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity" >
</activity>
</application>


Related Topics



Leave a reply



Submit