[JAVA] Android development ~ Screen transition (intent) ~

Introduction

Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well. The theme this time is * "Intent" *.

Target person

--A person who can write Java somehow. --Someone who is ambiguous in Android development but can do it a little.

Two types of intents

--Explicit intent → Specify the activity class to start.

--Implicit intent → Specify the URI and action (described later) that indicate what kind of activity you want to launch. The Android OS finds and launches the corresponding activity based on the URI and action. If there are multiple launch destinations, the app chooser will be displayed.

Now let's see how to handle it.

Explicit intent

To make a screen transition with an explicit intent: ** 1) Register the activity in AndroidManifest.xml **

<?xml version="1.0" encoding="utf-8"?>
<manifest ...... >

    <application ........ >
        <activity>

~ Main activity ~

        </activity>

        <!--add to-->
        <activity android:name="Package name + class name">

    </application>

</manifest>

*If the activity class is directly under the root package, "."Class name" is OK!

** It is convenient if you create the above procedure from "File> New> Activity> Empty Activity". *

** 2) Start screen ** ○ How to start another screen from the current screen. (1) Intent object generation.

~~~~~~~~ = new Intent(context,Launched activity)

(2) Execute the ** startActivity () ** method with (1) as an argument.

** 3) Data passing ** ○ Use ** putExtra ("name", value) ** to pass data to the startup activity.

** 4) Receive data ** ○ The method of receiving data in the startup activity is as follows. ① Get Intent object

Intent intent = getIntent();

② Get Bundle object

Bundle extra = intent.getExtra();

③ Get data using get data type method of Bundle ForResult ○ When processing with the original activity after the startup activity ends ** 1) The following method to start the activity **

startActivityForResult(Intent object,Request code)

** 2) The following method immediately before finish () of the startup activity **

setResult(Result code,Intent object)

** Result code *

** 3) Execute 1) and 2) and execute the following method in the original activity. ** **

onActivityResult()

3 arguments

--int requestCode: 1) integer value specified by the second argument --Int resultCode: 2) constant value specified by the first argument

Sample code

ForResultSampleActivity.java(Launch source)


Intent intent = new Intent(ForResultSampleActivity.this,RatingEvaluateActivity.class);
inten1.putExtra("name", name);
startActivityForResult(intent, RATING_EVALUATE);

RatingEvaluateActivity.java(Startup destination)


Intent intent = getIntent();
String name = intent.getStringExtra("name");

Implicit intent

The procedure for launching other apps with an implicit intent is as follows. ** 1) Create a URI object. ** **

Uri uri = Uri.porse(URI string(See below));

** 2) Create an Intent object. ** **

Intent intent = new Intent(A constant that represents an action.(See below), uri);

** 3) Launch the activity. ** **

startActivity(intent) ;

URI The URI of the Android OS standard application is as follows.

・ Browser → http: // ....., https: // ..... ・ Map → geo: Latitude, longitude geo: o, o? g = search string

(note) Japanese search keyword is Encode with URLEncoder.encode ([keyword], [encode format (URF-8, etc.)]).

・ Telephone → tel: Phone number

action

Use the constant field of the Intent class.

--ACTION_VIEW → Screen display --ACTION_CALL → Make a call based on the data. --ACTION_DIAL → Display the screen to make a call --ACTION_SEND → Send email / SMS

Sample code

① Transition to the map application

try {
     TextView etKeyword = findViewById(R.id.etKeyword);
     String keyword = etKeyword.getText().toString();
     keyword = URLEncoder.encode(keyword, "utf-8");
     Uri uri = Uri.parse("geo:0,0?q=" + keyword);
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
} catch(UnsupportedEncodingException ex) {
     Log.e("MapSearchActivity", "keyword conversion failure", ex);
}

② Transition to the browser

String url = "http:://www.~~~.~~";
Uri uri = Uri.parse(url);

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

that's all. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.

Recommended Posts

Android development ~ Screen transition (intent) ~
Screen transition using Intent in Kotlin
Screen transition method
Screen transition memorandum
Android Development App_Preparation
JavaFX8 screen transition
Screen transition by [Android Studio] [Java] button
Rails Development Transition 2020
Android development link summary
Android application development preparation 7/15
Android development reference site
[Android / Java] Screen transition and return processing in fragments
iOS engineer starts Android development
[Swift] Simple screen transition summary
Android app personal development kickoff
Introduction to Android application development
ROS app development on Android
[Android] Exit the activity of the transition source at the time of screen transition
Screen transition Information transfer memorandum
Animation settings at screen transition
Screen transition with swing, java
About the basics of Android development
[Rails] How to prevent screen transition
First Android development for busy people
Screen transition by Post method [Java]
Library collection useful for Android development
[Java Swing] Screen transition by CardLayout
Notes for Android application development beginners
Problems with android studio development series
[Android] Receive intent on Broadcast Reciever
Android application: Let's explain the mechanism of screen transition with simple code
[Android application development] How to display in full screen (notification bar hidden)