Android Development Series 2 - details of Activity page Jump

Keywords: Android xml encoding

In Android development, the jump between Activity pages can be divided into two types. There are different methods and methods in two of them. Next, we will introduce each method of page Jump in detail.

Intent is a messaging object that can be used for other application request operations. Intent can promote the communication between components in many ways. Next, I will only explain the part where intent enables Activity and page Jump.

Intent consists of the following components:

Component: destination component
Action: action used to express intention
Category: category used to represent an action
Data: data to be manipulated for presentation and action
type: description of the data example
extras: extended information
Flags: the mode in which this intention is expected to operate

The type of Intent can be divided into explicit Intent (direct type) and implicit Intent (indirect type).

Activity represents a screen in the application. You can start a new activity instance by passing intent to startActivity(). Intent is used to describe the activity to start and carry any necessary data. If you want to receive results after the activity completes, call startActivityForResult(). In the onActivityResult() callback of activity, your activity receives the result as a separate intent object.

1, Show call methods

Current page A is named: MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Page to next page
        Button nextBt = findViewById(R.id.nextBtn);
        nextBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	 // Jump to the next page
				 Intent inter = new Intent(MainActivity.this, NextActiverVC.class);
        		startActivity(inter); 
            }
        });
    }
  }

The jump page B is named NextActiverVC

public class NextActiverVC extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        
        // A back button on the page
        Button gobackBtn = findViewById(R.id.button);
        gobackBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
            // Back to previous page
                finish();
            }
        });
        System.out.println("--nextVC----onCreate--------start----");
    }
}

1. Single page Jump

1) Intent direct class method

Intent inter = new Intent(this, NextActiverVC.class);
startActivity(inter);

//perhaps

Intent inter = new Intent(MainActivity.this, NextActiverVC.class);
startActivity(inter);

// perhaps

Intent inter = new Intent();
inter.setComponent(new ComponentName(this, NextActiverVC.class));
startActivity(inter);

2) Intent object setting method

Intent inter = new Intent();
inter.setClass(this,NextActiverVC.class);
startActivity(inter);

// perhaps

Intent inter = new Intent();
inter.setClass(MainActivity.this,NextActiverVC.class);
startActivity(inter);

// perhaps

Intent inter = new Intent();
inter.setComponent(new ComponentName(MainActivity.this, NextActiverVC.class));
startActivity(inter);

Where component component is used: target component

2. Multi level page Jump

The jump page C is named: ThirtvcActivity

public class ThirtvcActivity extends AppCompatActivity {

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

Directly jump to page C with page A, and the middle page is page B
Activity A-->ActivityB -> Activity C
How to jump:

		Intent intents[] = new Intent[2];
        Intent intent1 = new Intent(this,NextActiverVC.class);
        Intent intent2 = new Intent(this, ThirtvcActivity.class);
        
        intents[0] = intent1;
        intents[1] = intent2;
        
        startActivities(intents);

3. Page Jump callback parameter transfer

Use startActivityForResult from page A (MainActivity) to jump to page B (NextActivity). Click B to return and pass the newly written value back to page A through Intent.

public class MainActivity extends AppCompatActivity {

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

        Button nextBtn = findViewById(R.id.mainBtn);
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ///Button next
                Intent intent = new Intent(MainActivity.this, NextActivity.class);
                startActivityForResult(intent, 3);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 3 && resultCode == 5){
            String result = data.getStringExtra("resultStr");
            System.out.println(result);
        }

    }
}
public class NextActivity extends AppCompatActivity {

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

        final Button finishBtn = findViewById(R.id.finishBtn);

        finishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent finishIntent = new Intent();
                finishIntent.putExtra("resultStr", "Test return data");
                setResult(5,finishIntent);

                finish();
            }
        });

    }

// Avoid returning to the previous Activity page by clicking the return key. Data return cannot be realized.
// There are two ways to click the back button to return parameters

// Method 1: before calling back the return button, set the parameters of Intent to be returned
// When using method 2, method 1 is not called.
    @Override
    public void onBackPressed() {

        Intent finishIntent = new Intent();
        finishIntent.putExtra("resultStr", "Test return data");
        setResult(5,finishIntent);

        finish();

        System.out.println("--------------onBackPressed--------------");

        super.onBackPressed();
    }


// Method 2: by listening to the button, judge and click the return button keycode_backto set the return parameters.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        System.out.println("--------------onKeyDown--------------");

        if (keyCode == KeyEvent.KEYCODE_BACK){

            Intent finishIntent = new Intent();
            finishIntent.putExtra("resultStr", "Test return data");
            setResult(5,finishIntent);

            finish();

            return true;

        }else{
            return super.onKeyDown(keyCode, event);
        }
    }
}

Be careful:

The Back return key of the system directly returns to result ﹣ canclosed, without data parameters. If you click the return key to return data, you must rewrite or intercept the event processing of the Back key.

2, Implicit call

Configure the implicit method call in the Android manifest.xml file. The corresponding class and class name are consistent with the above.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ftimage.firstandroid">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ThirdvcActivity">
           
            <intent-filter>
                <action android:name="com.ftimage.firstandroid.test"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
            
        </activity>
        <activity android:name=".NextActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The method of page Jump is as follows. Write in detail.

// Jump method
Intent intent = new Intent("com.ftimage.firstandroid.test");
startActivity(intent);

Be careful:
In the implicit call process, Action is used to represent the Action of intention, and category is used to represent the category of Action. Other data, type, extras, etc. are configured as required, which is described in detail here.

In the implicit call process, the current App A can jump to other App B's pages and carry corresponding parameters. This example is just a jump in the App.

Continuously updating

61 original articles published, 18 praised, 80000 visitors+
Private letter follow

Posted by psymonic on Wed, 26 Feb 2020 21:48:21 -0800