Activity working in Android application.

Hello Friend,
Today, we are going to learn about activity in android.

What is an Activity ?
In simple language our mobile screen is activity in android.Activity is use to handle UI.
We need to understand activity consist multiple views and fragment.
Views means nothing else it's our weight like button,textview etc..


Simple Demo Jump one Activity to another activity in android.


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.spider.fragment_activity.MainActivity">
    <Button android:id="@+id/one" android:layout_width="100dp" android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" android:layout_marginTop="10dp"
        android:gravity="center" android:text="Second Activity" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button one;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        one = (Button) findViewById(R.id.one);
     
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
}
}

second.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        android:id="@+id/two"/>
</android.support.constraint.ConstraintLayout>

SecondActivity.java

public class SecondActivity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Button two = (Button)findViewById(R.id.two);
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SecondActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
}


OUTPUT:-




Comments