Android application: Let's explain the mechanism of screen transition with simple code

Last time Android app: Try to summarize events and listeners

Continued. .. ..

Introduction

It's been three months since I joined the company. I am studying android application development. I will summarize it after studying.

I am chewing on my own introductory level knowledge. Please note that there are many parts that are broken.

Range to summarize this time

This time, I will summarize the screen transition with a simple code. ・ I want to know the basics of screen transitions. ・ I want to understand the intent simply. For those who say.

What you create is an application that displays the entered text after the screen transition when you enter the text and press the send button.

Words that come out this time

--Intent

Simply put, it's like a container for carrying information from one activity to another. If you put the character string in the activity on screen 1 in a box called intent like this time, you can use the character string on screen 2 by calling the box on screen 2.

Words before the last time

--Event

The moment of user screen operation such as pressing a button

--Listener

Keep an eye on the event so that it can happen at any time.

--Activity

The app screen itself.

Sample & commentary

environment

androidstudio 3.6.2 openjdk version "11.0.6"

Screen to create

This is the screen to be created this time. シンプル画面遷移.jpg

I will write so that the entered characters can be displayed at the transition destination when the button is pressed. Create two required java classes and two layout files.

MainActivity.java


public class MainActivity extends AppCompatActivity {

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

        //Set the submit button as a listener
        Button btClick = findViewById(R.id.btOutput);
        Listener listener = new Listener();
        btClick.setOnClickListener(listener);
    }


    //Create a listener class
    private class Listener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            EditText input = findViewById(R.id.sendEditText);
            String inputStr = input.getText().toString();

            //Create an intent object
            Intent intent = new  Intent(MainActivity.this,OutputActivity.class);
            //Store data to be sent to screen ②
            intent.putExtra("inputText",inputStr);
            //Launch screen
            startActivity(intent);
        }
    }
}

OutputActivity.java



public class OutputActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub_activity);

        //Get the intent object created on screen ①
        Intent intent = getIntent();
        //Get character information and output to screen
        TextView output = findViewById(R.id.outputText);
        String outputText = intent.getStringExtra("inputText");
        output.setText(outputText);
    }
}

activity_main.xml


<LinearLayout ~~ Omitted ~~>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title"/>
    <EditText
        android:id="@+id/sendEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btOutput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="To the next screen"/>
</LinearLayout>

sub_activity.xml


<LinearLayout ~~ Omitted ~~>

    <TextView
        android:id="@+id/outputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40dp"/>

</LinearLayout>

About the mechanism and intent

Application startup: MainActivity processing (the first screen is displayed by this processing) ↓ Send button event occurrence: OutputActivity processing (second screen is displayed)

In other words, when this submit button event occurs, the string data entered by the user is sent from the MainActivity to the OutputActivity using an intent. The intent will be prepared when the event occurs, so write it in the listener class.

The part that puts data in a box called an intent

MainActivity.java



//Create an intent object
Intent intent = new  Intent(MainActivity.this,OutputActivity.class);
//Store data to be sent to screen ②
intent.putExtra("inputText",inputStr);
//Launch screen
startActivity(intent);

Create an intent object ↓ Store the data stored in the intent with putExtra ↓ Launch the next screen

When creating an intent object, describe the Activity class of the original screen in the first argument and the Activity class of the screen you want to send in the second argument.

SubActivity.java



//Get the intent object created on screen ①
Intent intent = getIntent();
//Get character information and output to screen
TextView output = findViewById(R.id.outputText);
String outputText = intent.getStringExtra("inputText");
output.setText(outputText);

The calling part on the second screen. You can call the intent object created on the initial screen with getIntent. All you have to do now is write the process to display the character string.

Caution

In the project folder, there is a file called AndroidManifest that contains the information required to set up Android. If there are two or more screens, the app will not work unless you add the Activity name here. Here, add " </ activity>" as shown below.


<application ~~ Omitted ~~>
        <activity android:name=".MainActivity">
~~ Omitted ~~
        </activity>

        <activity android:name=".OutputActivity"></activity>
</application>

At the end

I finished two introductory books in about a month. Although my understanding gradually progresses, I'm still an amateur.

Next time, I will summarize about the life cycle

Recommended Posts

Android application: Let's explain the mechanism of screen transition with simple code
[Android] Exit the activity of the transition source at the time of screen transition
[Android Studio] [For beginners] Let's roughly explain the screen and directories
The story of tuning android apps with libGDX
[Android 9.0 Pie Java] LINE-style chat list screen → personal chat screen transition animation implemented (with sample application)
Let's experience the authorization code grant flow with Spring Security OAuth-Part 1: Review of OAuth 2.0
Implement the UICollectionView of iOS14 with the minimum required code.
Roughly the flow of web application development with Rails.
I will explain the difference between Android application development and iOS application development from the perspective of iOS engineers
Check the operation of two roles with a chat application
Let's take a look at the screen of Quant Analyzer!
Specify the character code of the source when building with Maven
Explain the benefits of the State pattern with a movie rating
Let's rewrite the C language assignment of the university with Node.js
[Swift] Simple screen transition summary
Android development ~ Screen transition (intent) ~
Screen transition with swing, java
The basics of the process of making a call with an Android app
A memo that containerizes the simple chat application of Node.js + socket.io
Let's create a TODO application in Java 5 Switch the display of TODO
[Android] Dynamically set the height of ListView with copy (for beginners)
[Beginner] Let's write REST API of Todo application with Spring Boot