Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android new Intent with back button

In my Android App I use an empty activity with one button. If I push the button I call a new activity with an intent. But on the new activity the back button (top left) to jump back to the last page is missing. Isn't there already a activity template from android studio or a spezial intent call to do this? Or what is the best way to do this?

This is the function which is called on the button press

void scanClicked(View v){
    Intent intent = new Intent(this, ServiceActivity.class);
    startActivity(intent);
}

The ServiceActivity is just an Empty Activity

    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login"
        android:theme="@style/SplashTheme"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NavigationActivity"
        android:label="@string/title_activity_navigation"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".ServiceActivity"
        android:label="@string/annualServiceTitle">
    </activity>
like image 203
WeSt Avatar asked Jul 14 '26 15:07

WeSt


1 Answers

There are 2 options to do that.

Either you can use the android ToolBar in your XML Adding toolbar

Or you can add a Layout which has a ImageButton aligned to the left of the layout. Then you can do this.

ImageButton backBtn;

backBtn = (ImageButton) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

Hope this helps.

like image 174
Sarthak Gandhi Avatar answered Jul 16 '26 06:07

Sarthak Gandhi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!