Thursday 20 October 2016

Explicit Intent

Unequivocal purposes indicate the segment to begin by name (the completely qualified class name). You'll normally utilize an unequivocal plan to begin a part in your own particular application, since you know the class name of the movement or administration you need to begin. For instance, begin another action because of a client activity or begin an administration to download a document out of sight.

Syntax : -  By using Intent class
                  Intent in=new Intent(Currentclass.this,TargetActivity.class);
                  startActivity(in);

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity"     
 android:background="#868226">
<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="call to second activity" 
 android:id="@+id/button" 
 android:layout_marginTop="20dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#ca4b4b" />

</LinearLayout>

MainActivity.java :-

package com.kiranapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MEnuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

 Button entersecond;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 entersecond=(Button)findViewById(R.id.button);
 entersecond.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
 Intent in=new Intent(MainActivity.this,secondactivity.class);
 startActivity(in);
 }
 });
 }
}
 
Note :-  

In this code i taken one activty and second activity only.

 For secondactivity.class we have to create one java class i.e under java give right click and create the class name i.e "secondactivity.java".

 Similarly in layout i.e second_activity.xml also should create the layout.
      Shown below figure 2 i.e you enter second activity (Text View).

If we want more activities we can create " java class and xml "


Output :-

Explicit Intent
Explicit Intent fig :1





















Explicit Intent
Explicit Intent fig :2













No comments:

Post a Comment