To create a new activity, you must follow these steps:
Create a new class in your package that inherits from the Activity class.
Generate the onCreate () method.
Create a new layout file, and add the desired graphics.
Associate the layout file to your activity in the onCreate () method.
2. To start another activity, you must run the method :
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp">
</TextView>

android:layout_width="70dp"
android:layout_height="50dp"
android:text="Act2"
android:textSize="30dp"
android:background="@drawable/bpassage"
<Button
android:id="@+id/toact3"
android:layout_width="70dp"
android:layout_height="50dp"
android:text="Act3"
android:textSize="30dp"
android:background="@drawable/bpassage"
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class act1 extends Activity {
/** Called when the activity is first created. */
private Button act2;
private Button act3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
getIntent();
act2 = (Button) findViewById(R.id.toact2);
act3 = (Button) findViewById(R.id.toact3);
act2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent (act1.this, act2.class);
startActivity(i);
}
});
act3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i1 = new Intent (act1.this, act3.class);
startActivity(i1);
finish();
}
});
}
}
Create a new class in your package that inherits from the Activity class.
Generate the onCreate () method.
Create a new layout file, and add the desired graphics.
Associate the layout file to your activity in the onCreate () method.
the purpose of this article is to manage a passage from one activity to another
The passage between two activities requires an Intent. An Intent is a container for information.
It used to pass messages between two activities.
The appellant activity can thus transmit information to the application called,and the Android system.
- There are several ways to create an Intent. We will choose the following:
MyIntent intent = new Intent (<context>, < class of the activity target);
startActivity (Intent i) the initial Activity class.
3. In the target activity, we get the Intent :
getIntent();
<TextView
android:text="Activity 1" android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp">
</TextView>

<Button
android:id="@+id/toact2"android:layout_width="70dp"
android:layout_height="50dp"
android:text="Act2"
android:textSize="30dp"
android:background="@drawable/bpassage"
/>
<Button
android:id="@+id/toact3"
android:layout_width="70dp"
android:layout_height="50dp"
android:text="Act3"
android:textSize="30dp"
android:background="@drawable/bpassage"
/>
we will create an application that consists of 3 Activity, each activity has the same
graphical interface
Activity 1 :
package electro.droid;
import android.app.Activity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class act1 extends Activity {
/** Called when the activity is first created. */
private Button act2;
private Button act3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
getIntent();
act2 = (Button) findViewById(R.id.toact2);
act3 = (Button) findViewById(R.id.toact3);
act2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent (act1.this, act2.class);
startActivity(i);
}
});
act3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i1 = new Intent (act1.this, act3.class);
startActivity(i1);
finish();
}
});
}
}
- we will copy this code and paste it in activity number 2 , 3.
2. we have to change the activity target :
- Example in the second activity :
startActivity(i1);
- activity number 3 :
Intent i1 = new Intent (act3.this, act1.class); // from 3 to 1
startActivity(i1);
startActivity(i1);
do not keep <intent-filter> tag: This tag allows
intent.action.MAIN,
to specify that the current activity is the initial
activity of the application..
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".act1"android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".act2" android:label="@string/app_name">
<activity android:name=".act1"android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".act2" android:label="@string/app_name">
</activity>
<activity android:name=".act3" android:label="@string/app_name">
</activity>
</application>
</application>





0 comments:
Post a Comment