'How can I get android to stop skipping main activity and keep functionality working?
I'm practicing Android Studio and I'm stuck on this part. The little app I built has two screens, the first has a welcome message and a button that when clicked will send the user to the second screen (the second activity). This second screen has four buttons, each of which is marked with a value (1 to 4). When the buttons are pressed their values are added to an ArrayList of integer values, those values are then sent back to the main activity and if they match the right order and size, the message on the main screen changes. I managed to get that working, except for matching the right numbers in order part. My issue now is that my app skips the main activity and goes straight to the second activity when launched. And when the array list is sent back to the main activity, and I go back to enter a new combination in the second activity and go back to the main activity, nothing changes anymore. How can I fix the activity skipping and also be able to cycle back and forth between the two activity and have them continously update depending on what happens on activity two?
Below is the MainActivity class code
package com.example.securityapp;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Integer> passcode = new ArrayList<Integer>(4);
TextView mainMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passcode.add(1);
mainMessage = findViewById(R.id.main_message);
final ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
Bundle extras = data.getExtras();
if(extras.getIntegerArrayList("keys").size() == 4) {
mainMessage.setText("Welcome to the App! The App is UNLOCKED!");
}
else {
mainMessage.setText("Welcome to the App! The App is LOCKED!");
}
}
}
}
);
Intent intent = new Intent(MainActivity.this , AccessControlActivity.class);
activityResultLauncher.launch(intent);
}
/** Called when user taps the Unlock button */
public void unlockApp(View view) {
Intent intent = new Intent(this, AccessControlActivity.class);
intent.putExtra("key", passcode);
startActivity(intent);
}
}
XML for MainActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_unlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="308dp"
android:onClick="unlockApp"
android:text="@string/btn_unlock"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/main_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="@string/main_message"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/btn_unlock"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.949" />
</androidx.constraintlayout.widget.ConstraintLayout>
Second activity (AccessControlActivity)
package com.example.securityapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
public class AccessControlActivity extends AppCompatActivity {
private static final int[] numbers = {R.id.key_1, R.id.key_2, R.id.key_3, R.id.key_4};
private static int[] passcode = new int[4];
private Button[] button = new Button[numbers.length];
ArrayList<Integer> numbersList = new ArrayList<Integer>();
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_access_control);
TextView textView = findViewById(R.id.textView2);
submit = findViewById(R.id.btn_submit);
for(int i = 0; i < numbers.length; i++) {
button[i] = (Button) findViewById(numbers[i]);
button[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.key_1:
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
numbersList.add(1);
break;
case R.id.key_2:
Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
numbersList.add(2);
break;
case R.id.key_3:
Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
numbersList.add(3);
break;
case R.id.key_4:
Toast.makeText(getApplicationContext(), "4", Toast.LENGTH_SHORT).show();
numbersList.add(4);
break;
}
textView.setText(String.valueOf(numbersList));
}
});
}
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent keycode = new Intent(AccessControlActivity.this, MainActivity.class);
keycode.putExtra("keys", numbersList);
setResult(RESULT_OK, keycode);
finish();
}
});
}
}
XML for AccessControlActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AccessControlActivity">
<Button
android:id="@+id/key_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="@string/key_3"
app:layout_constraintBaseline_toBaselineOf="@+id/key_2"
app:layout_constraintEnd_toStartOf="@+id/key_4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/key_2" />
<Button
android:id="@+id/key_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="312dp"
android:text="@string/key_1"
app:layout_constraintEnd_toStartOf="@+id/key_2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="onSubmit"
android:text="@string/btn_submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/key_3"
app:layout_constraintVertical_bias="0.042" />
<Button
android:id="@+id/key_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/key_4"
app:layout_constraintBaseline_toBaselineOf="@+id/key_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/key_3" />
<Button
android:id="@+id/key_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="@string/key_2"
app:layout_constraintBaseline_toBaselineOf="@+id/key_1"
app:layout_constraintEnd_toStartOf="@+id/key_3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/key_1" />
<TextView
android:id="@+id/key_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="@string/key_message"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/key_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.922" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pass_key"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="526dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Solution 1:[1]
The activity from which your app starts is declared in the android manifest. For example, in this code snippet, the launcher activity would be my splash activity.
<activity
android:name=".feature.planes.PlanDetailActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.Matatapp" />
<activity
android:name=".feature.splash.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.Matatapp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
But this doesn't appear to be your issue. You are currently launching an intent from your onCreate method, so even though you have MainActivity set as launcher, when your application launches MainActivity and goes through onCreate, it launches your AccessActivity so quickly you don't even notice. To test if MainActivity is being launched in first place, just run your app, and natively go back. If MainActivity is showed, then you know your issue is that you have to launch your intent from another method called when the user triggers something.
I would eagerly recommend you to look at ativity lifecycle.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
