Thursday, May 31, 2012

Starting New Activity from another activity

If you want to start new activity from existing activity then follow following steps:

1.   right-click on package in which you want to create new activity then after select new -> class -> enter class name -> finish

It create new class on your package as show below.

          public class SecondActivity {

          }

2.  Extend Activity class

public class SecondActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      }
}

3. Register newly created activity in your AndroidManifest.xml

           <activity  android:label="Test"
            android:name=".SecondActivity" >
        </activity>

4.  in main activity from you want to start new activity write following code

        - Create intent
                Intent intent=new Intent(this,SecondActivity.class);
       - start Activity
                 startActivity(intent);

No comments:

Post a Comment