Answer the question
In order to leave comments, you need to log in
When you press the back button, the application closes, but it is necessary that it goes to the main screen. What should be written in the code?
I am developing an Android application for the first time and I faced such a problem. I currently have two screens in the application, this is the main activity and the second screen for displaying information after the user clicks on the button on the main screen, the transition after clicking works as it should, but after I want to go back to the main screen (I click back button on the device), the application closes. What needs to be done to fix this.
MainActivity:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button UgnaiaVokzalMarhut2 = (Button)findViewById(R.id.rout2);
UgnaiaVokzalMarhut2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity.this, Ostanovki2.class);
startActivity(intent);
finish();
} catch (Exception e) {
} //Конец конструкции
}
});
}
}
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class Ostanovki2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ostanovki2);
}
}
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Ostanovki2"
android:label="@string/Ost2"
android:screenOrientation="portrait">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Answer the question
In order to leave comments, you need to log in
UgnaiaVokzalMarhut2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity.this, Ostanovki2.class);
startActivity(intent);
finish(); <--------------- Удоли
} catch (Exception e) {
} //Конец конструкции
}
});
В манифесте у второй Activity указать родительскую
<activity android:name=".Ostanovki2"
android:label="@string/Ost2"
android:screenOrientation="portrait"
android:parentActivityName=".MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question