Android.Activity

Register an activity in AndroidManifast. If you want to run an Activity, you must first register

<activity
            android:name=".FirstActivity"
            android:label="This is my first Activity" //title bar
            >
            <intent-filter >

//Let FirstActivity, As main Activity
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

=== ========

Toast application


Toast.makeText(FirstActivity.this, "show Toast", Toast.LENGTH_LONG).show();
//.makeText returns the Toast object, and then calls the show method

FirstActivity.this, In which Activity is displayed

===================


Start the second Activity

and register the Activity first:
<activity
android:name=".SecondActivity" //class name       
            >
        </activity>


//Click the button to start another Activity
class Listener2 implements

OnClickListener { @Override
public void onClick(View v ) {
//FirstActivity.this, the current Activity
//SecondActivity.class, the Activity you want to start
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

startActivity(intent);//Start Activity
}

=== ==============================================================================================================================================================================================

_

_

_ /Specify the data to be manipulated by the Intent object
//Generally transmitted through a string, parsed by Uri
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);//Start Activity


===========================

Pass data to the next Activity


public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);

String data="1234qwer";

//Pass data to the next Activity
//key, key; data, value
intent.putExtra("key", data);
startActivity(intent);
}

//Get the passed data
//Get the Intent of the current Activity
Intent intent = getIntent();
String string = intent.getStringExtra("key ");//Get the passed data
Log.d("SecondActivity", string);
Toast.makeText(SecondActivity.this, string, Toast.LENGTH_LONG).show();

===========================

Return data to the previous Activity

step1
public void onClick(View v) {

Intent intent = new Intent(FirstActivity.this , SecondActivity.class);
startActivityForResult(intent, 1);// Return data to the previous Activity
}

step2
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("key1", " value1");

setResult(RESULT_OK, intent);//Up to an Activity, return data
finish();//Destroy Activity
}

step3
//
Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch ( requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String string = data.getStringExtra("key1");
Log.d("SecondActivity", string);
}
break;

default:
break;
}
}

========== =======================

Know which Activity is currently in

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Get the current instance The class name
// and print it out through log
Log.d("MainActivity", getClass().getSimpleName());
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327055056&siteId=291194637