Tuesday, November 19, 2013

Transition from Activity to another

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.

 

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.

  1. There are several ways to create an Intent. We will choose the following:
                 MyIntent intent = new Intent (<context>, < class  of the activity target);

      2.  To start another activity, you must run the method :

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

  1.  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 :
                       Intent i1 = new Intent (act2.this, act3.class); // from 2 to 3
                       startActivity(i1);

  • activity number 3 :
                       Intent i1 = new Intent (act3.this, act1.class); // from 3  to 1
                       startActivity(i1);


      3.  Add new activity in the Manifest file. For this new activity in the manifest, 
           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>
               <activity android:name=".act3" android:label="@string/app_name">
               </activity>

    </application>

HMAIDA.H

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 ELECTRO DROID.

Designed by Atef.A | Deal Commerce