Android App Development With AndroidCource: activity
Showing posts with label activity. Show all posts
Showing posts with label activity. Show all posts

Thursday, 14 September 2017

Android Activity

What is Activity ?

An activity represents a single screen with a user interface just like window or frame of Java.The help of activity, you can place all your UI components or widgets in a single screen.


Activity LifeCycle


To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of seven callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(),onRestart() and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.


List out Activity  Lifecycle methods


onCreate()

This is the first callback and called when the activity is first created.

onStart()

This callback is called when the activity becomes visible to the user.

onResume()

This is called when the user starts interacting with the application.

onPause()

This callback is called when activity is not visible to the user.

onStop()

This callback is called when the activity is no longer visible.

onRestart()

This callback is called when the activity restarts after stopping it.

onDestroy()

This callback is called before the activity is destroyed by the system.





Monday, 21 August 2017

Pass Data to One Activity to Another Activity

MainActivity.xml
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_pass_data_activitity"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#cfdb28"
                tools:context="com.bhaumik.programmingstudy.MainActivitity">

    <EditText
            android:id="@+id/et_name"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="#FFF"
            android:padding="10dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"/>
    <Button
            android:id="@+id/btn_showText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Text"
            android:textAllCaps="false"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/et_name"
            android:layout_marginTop="30dp"/>
</RelativeLayout>
ReceiveDataActivity.xml
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_receive_data"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:background="#168080"
                tools:context="com.bhaumik.programmingstudy.ReceiveDataActivity">

    <TextView
            android:id="@+id/tv_resultText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:textColor="#FFF"
            android:textStyle="bold"
            android:textSize="20dp" />
</RelativeLayout>
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PassDataActivitity extends AppCompatActivity {

    EditText name;
    Button btn_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass_data_activitity);

        name = (EditText) findViewById(R.id.et_name);
        btn_show = (Button) findViewById(R.id.btn_showText);

        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String names = name.getText().toString();
                Intent intent = new Intent(MainActivitity.this,ReceiveDataActivity.class);
                intent.putExtra("Names",names);
                startActivity(intent);
            }
        });
    }
}
ReceiveDataActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveDataActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_data);

        String type = getIntent().getStringExtra("Names");
        textView = (TextView) findViewById(R.id.tv_resultText);
        textView.setText(type);
    }
}

Ads