01 Learning JavaScript
01 Learning JavaScript
It can be used with startActivity to launch an Activity, broadcast Intent to send it to any
interested BroadcastReceiver components, and startService(Intent) or bindService(Intent,
ServiceConnection, int) to communicate with a background Service.
"The intent itself, an Intent object, is a passive data structure holding an abstract description
of an operation to be performed."
Intent-Filter:
Specifies the types of intents that an activity, service, or broadcast receiver can respond to. An
intent filter declares the capabilities of its parent component — what an activity or service can
do and what types of broadcasts a receiver can handle. It opens the component to receiving
intents of the advertised type, while filtering out those that are not meaningful for the
component.
Most of the contents of the filter are described by its <action>, <category>, and <data>
subelements.
In Android user interface is displayed through an activity. In Android app development you
might face situations where you need to switch between one Activity (Screen/View) to another.
In this tutorial I will be discussing about switching between one Activity to another and sending
data between activities.
i.putExtra("key", "value");
Intent i = getIntent();
i.getStringExtra("key");
In some situations you might expect some data back from newly created activity. In that
situations startActivityForResult() method is useful. And once new activity is closed you should
you use onActivityResult() method to read the returned result.
Closing Activity:
finish();
3) Create XML file and named it as activity_main.xml (if not created already)
Important Note: Do not remove your package name in a file that is written on top of the files.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
import android.util.Log;
// Initializing variables
EditText inputName;
EditText inputEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(nextScreen);
}
});
}
}
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You Entered..."
android:textSize="25dip"
android:gravity="center"
android:layout_margin="15dip"/>
<TextView android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dip"
android:textSize="18dip"/>
<TextView android:id="@+id/txtEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dip"
android:textSize="18dip"/>
<Button android:id="@+id/btnClose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="Close"/>
</LinearLayout>
7) Create SecondScreenActivity.java and add following code in it.
Important Note: Do not remove your package name in a file that is written on top of the files.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Hp on 9/15/2016.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
String email = i.getStringExtra("email");
Log.e("Second Screen", name + "." + email);
8) Open AndroidManifest.xml and add following code before end of application tag.