Я создаю шестиуровневую игру на запоминание со спиннером для выбора уровней. Когда я выбираю уровень, он переходит к этому экрану на долю секунды, а затем каждый раз возвращается к основному экрану активности. Как мне настроить его, чтобы он оставался на выбранном экране?
Вот главный XML:
<?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">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:entries="@array/menu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="32dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="32dp"
android:text="This is Daytona!"
android:textSize="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Вот MainActivity. java
public class MainActivity extends AppCompatActivity {
Spinner spinner;
int currentItem = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
//String text = spinner.getSelectedItem().toString();
Intent intent;
switch(position) {
case 0:
return;
case 1:
intent = new Intent(MainActivity.this, Charlotte.class);
startActivity(intent);
finish();
break;
case 2:
intent = new Intent(MainActivity.this, Bristol.class);
startActivity(intent);
finish();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
Вот Шарлотта. java
public class Charlotte extends AppCompatActivity {
Spinner spinner;
int currentItem = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charlotte);
spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
//String text = spinner.getSelectedItem().toString();
Intent intent;
switch(position) {
case 0:
intent = new Intent(Charlotte.this, MainActivity.class);
startActivity(intent);
finish();
break;
case 1:
return;
case 2:
intent = new Intent(Charlotte.this, Bristol.class);
startActivity(intent);
finish();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
И третья страница Бристоль. java
public class Bristol extends AppCompatActivity {
Spinner spinner;
int currentItem = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bristol);
spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
//String text = spinner.getSelectedItem().toString();
Intent intent;
switch(position) {
case 0:
intent = new Intent(Bristol.this, MainActivity.class);
startActivity(intent);
finish();
break;
case 1:
intent = new Intent(Bristol.this, Charlotte.class);
startActivity(intent);
finish();
break;
case 2:
return;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}