Advertisement
Combreal

dualActivity.java

Aug 6th, 2022
1,492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. -----------------------------AndroidManifest.xml
  2.  
  3.  
  4.  
  5. <?xml version="1.0" encoding="utf-8"?>
  6. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  7.     xmlns:tools="http://schemas.android.com/tools"
  8.     package="com.example.testdualactivities">
  9.  
  10.     <application
  11.         android:allowBackup="true"
  12.         android:dataExtractionRules="@xml/data_extraction_rules"
  13.         android:fullBackupContent="@xml/backup_rules"
  14.         android:icon="@mipmap/ic_launcher"
  15.         android:label="@string/app_name"
  16.         android:roundIcon="@mipmap/ic_launcher_round"
  17.         android:supportsRtl="true"
  18.         android:theme="@style/Theme.TestDualActivities"
  19.         tools:targetApi="31">
  20.  
  21.         <activity android:name=".DisplayMessageActivity"
  22.             android:parentActivityName=".MainActivity">
  23.         <meta-data
  24.             android:name="android.support.PARENT_ACTIVITY"
  25.             android:value=".MainActivity" />
  26.         </activity>
  27.  
  28.         <activity
  29.             android:name=".MainActivity"
  30.             android:exported="true">
  31.             <intent-filter>
  32.                 <action android:name="android.intent.action.MAIN" />
  33.  
  34.                 <category android:name="android.intent.category.LAUNCHER" />
  35.             </intent-filter>
  36.         </activity>
  37.     </application>
  38.  
  39. </manifest>
  40.  
  41.  
  42.  
  43. -----------------------------activity_main.xml
  44.  
  45.  
  46.  
  47. <?xml version="1.0" encoding="utf-8"?>
  48. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  49.     xmlns:app="http://schemas.android.com/apk/res-auto"
  50.     xmlns:tools="http://schemas.android.com/tools"
  51.     android:layout_width="match_parent"
  52.     android:layout_height="match_parent"
  53.     tools:context=".MainActivity">
  54.  
  55.     <Button
  56.         android:id="@+id/startActivityButton"
  57.         android:layout_width="wrap_content"
  58.         android:layout_height="wrap_content"
  59.         android:text="Start 2nd Activity!"
  60.         app:layout_constraintBottom_toBottomOf="parent"
  61.         app:layout_constraintEnd_toEndOf="parent"
  62.         app:layout_constraintStart_toStartOf="parent"
  63.         app:layout_constraintTop_toTopOf="parent" />
  64.  
  65. </androidx.constraintlayout.widget.ConstraintLayout>
  66.  
  67.  
  68.  
  69. -----------------------------MainActivity.java
  70.  
  71.  
  72.  
  73. package com.example.testdualactivities;
  74.  
  75. import static android.provider.AlarmClock.EXTRA_MESSAGE;
  76.  
  77. import androidx.appcompat.app.AppCompatActivity;
  78.  
  79. import android.content.Intent;
  80. import android.os.Bundle;
  81. import android.view.View;
  82. import android.widget.Button;
  83. import android.widget.EditText;
  84. import android.widget.Toast;
  85.  
  86. public class MainActivity extends AppCompatActivity {
  87.     public static final String EXTRA_MESSAGE = "com.example.testdualactivities.MESSAGE";
  88.     Button mStartActivityBnt;
  89.  
  90.     @Override
  91.     protected void onCreate(Bundle savedInstanceState) {
  92.         super.onCreate(savedInstanceState);
  93.         setContentView(R.layout.activity_main);
  94.  
  95.         mStartActivityBnt = findViewById(R.id.startActivityButton);
  96.  
  97.         mStartActivityBnt.setOnClickListener(new View.OnClickListener() {
  98.             @Override
  99.             public void onClick(View v) {
  100.                 Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
  101.                 String message = "Here, have some text.";
  102.                 intent.putExtra(EXTRA_MESSAGE, message);
  103.                 startActivity(intent);
  104.             }
  105.         });
  106.     }
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113. -----------------------------activity_display_message.xml
  114. <?xml version="1.0" encoding="utf-8"?>
  115. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  116.     xmlns:app="http://schemas.android.com/apk/res-auto"
  117.     xmlns:tools="http://schemas.android.com/tools"
  118.     android:layout_width="match_parent"
  119.     android:layout_height="match_parent"
  120.     tools:context=".DisplayMessageActivity">
  121.  
  122.     <TextView
  123.         android:id="@+id/textView"
  124.         android:layout_width="wrap_content"
  125.         android:layout_height="wrap_content"
  126.         android:text="TextView"
  127.         app:layout_constraintBottom_toBottomOf="parent"
  128.         app:layout_constraintEnd_toEndOf="parent"
  129.         app:layout_constraintStart_toStartOf="parent"
  130.         app:layout_constraintTop_toTopOf="parent" />
  131. </androidx.constraintlayout.widget.ConstraintLayout>
  132.  
  133.  
  134.  
  135. -----------------------------DisplayMessageActivity.java
  136.  
  137.  
  138.  
  139. package com.example.testdualactivities;
  140.  
  141. import androidx.appcompat.app.AppCompatActivity;
  142.  
  143. import android.content.Intent;
  144. import android.os.Bundle;
  145. import android.widget.TextView;
  146.  
  147. public class DisplayMessageActivity extends AppCompatActivity {
  148.  
  149.     @Override
  150.     protected void onCreate(Bundle savedInstanceState) {
  151.         super.onCreate(savedInstanceState);
  152.         setContentView(R.layout.activity_display_message);
  153.  
  154.         Intent intent = getIntent();
  155.         String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  156.         TextView textView = findViewById(R.id.textView);
  157.         textView.setText(message);
  158.  
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement