Is It Ok to Start New Activity on Back Pressed

Add a Back Button to Action Bar Android Studio (Kotlin)

      DESCRIPTION

In this tutorial we will add a back button in action bar, when it is clicked it will go to previous activity(the app will close if this was launcher activity). We will go from main activity to new activity by clicking button in main activity. In new activity we will add a back button to actionbar when that button is clicked the main activity will appear...

      VIDEO


      SOURCE CODE

Step 1: Create a new Project oropen new project

Step 2: Create New Activity File>New>Activity>EmptyActivity

Step 3: Code:

activity_main.xml

                  <?xml version="1.0" encoding="utf-8"?>                  <LinearLayout                  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"                  android:gravity=                  "center"                  tools:context=                  ".MainActivity"                  >                  <Button                  android:id=                  "@+id/startActBtn"                  android:text=                  "New Activity"                  android:layout_width=                  "wrap_content"                  android:layout_height=                  "wrap_content"                  />                  </LinearLayout>                

MainActivity.kt

                  package                  com.blogspot.devofandroid.myapplication                  import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button                  class                  MainActivity                  : AppCompatActivity() {                  override                  fun                  onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)                  //button                  val                  mStartActBtn = findViewById<Button>(R.id.startActBtn)                  //handle button click                  mStartActBtn.setOnClickListener {                  //start activity intent                  startActivity(Intent(this@MainActivity, NewActivity::class.java))         }      }  }                

activity_new.xml

                  <?xml version="1.0" encoding="utf-8"?>                  <LinearLayout                  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"                  android:orientation=                  "vertical"                  android:gravity=                  "center"                  tools:context=                  ".NewActivity"                  >                  <TextView                  android:text=                  "Click back buttoon of actionbar"                  android:layout_width=                  "wrap_content"                  android:layout_height=                  "wrap_content"                  />                  </LinearLayout>                

NewActivity.kt

                  package                  com.blogspot.devofandroid.myapplication                  import android.support.v7.app.AppCompatActivity import android.os.Bundle                  class                  NewActivity                  : AppCompatActivity() {                  override                  fun                  onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_new)                  //actionbar                  val                  actionbar = supportActionBar                  //set actionbar title                  actionbar!!.title =                  "New Activity"                  //set back button                  actionbar.setDisplayHomeAsUpEnabled(true)         actionbar.setDisplayHomeAsUpEnabled(true)     }                  override                  fun                  onSupportNavigateUp(): Boolean {         onBackPressed()                  return                  true                  } }                

Step 4: Run Project

Output

Add a Back Button to Action Bar android studio (Kotlin)

Popular posts from this blog

AlertDialog with custom layout (Kotlin)

Image

How to create Create AlertDialog With Custom Layout (Kotlin)?      DESCRIPTION      This tutorial will show how to create and show an AlertDialog with C ustom Layout containing views such as EditTexts and Buttons etc. We will show AlertDialog on Button click. Custom layout will contain three EditTexts and two Buttons . When information is entered in EditTexts , Press Login Button , Dialog will be dismissed, and the information will be set to the TextView .      VIDEO           SOURCE CODE      Step 1:  Create a new Project  or  open new project Step 2: Create new Layout Resource file 'login_dialog.xml' under res>layout folder Step 3: Code activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" an

How to export an Android Studio project as a zip file?

Image

Export an Android Studio project as a ZIP file Starting with the Android Studio 3.0, you can use File | Export to Zip File... to export your project to zip or HTML easily. There is also a great advantage of exporting the project as a zip file, that is, it exports in very small size mostly in KBs. STEPS: Follow these steps to export your project in zip format: Step 1:  Create a new project   OR   Open your project . Step 2: From the Top menu Click File and then click Export to Zip File (Android Studio below 4.1) Step 2: From the Top menu click File > Manage IDE Settings > Export to Zip File (Android Studio 4.1 and above) Step 3: Choose/browse the directory/folder where you want to save the project as zip file, change the name if you want and click OK button. That's it, your project is now saved as zip file.

Is It Ok to Start New Activity on Back Pressed

Source: https://devofandroid.blogspot.com/2018/03/add-back-button-to-action-bar-android.html

0 Response to "Is It Ok to Start New Activity on Back Pressed"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel