unknown os platform
Android maintain the previous state of activity on intent | Whats-online.info
whats-online-info

Android maintain the previous state of activity on intent


Android
2017:01:29 / Science and Tutorials

Android tutorial, how to maintain the previous state of activity on intent to another activity and on go back button click programmatically. See example





Android maintain the previous state of activity on intent programmatically




Download source code example at the end of this tutorial



Hi, this another great android post with great information describing how you can maintain the previous state of activity on intent to another activity programmatically. I will show how to create a custom button that works like 'onBackPressed() function button' that allows one to maintain the previous state of activity ongoing back button is clicked. Besides, pass data from the second activity to the previous activity. 

How to go about it.
First, you need to create a new project.
Add the following classes to our project.

-MainActivity.java
-Activity2.java

Also, add the following XML files to our project.

-activity_main.xml
-activity2.xml

Now lets code to demonstrate this post.

Add the following code to layout our activities interfaces.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".MainActivity">   <EditText       android:layout_width="fill_parent"       android:layout_margin="30dp"       android:id="@+id/edtx"       android:layout_height="wrap_content" />     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:onClick="btnClick"         android:id="@+id/btnView"         android:text="VIew text"         />     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="btnClick"         android:layout_gravity="center"         android:id="@+id/btnIntent"         android:text="Intent to Activity 2"         />     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/viewtx"         android:layout_gravity="center"         android:textSize="25dp"         /> </LinearLayout>


activity2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context=".Activity2">     <EditText         android:layout_width="fill_parent"         android:layout_margin="30dp"         android:id="@+id/edtx"         android:layout_height="wrap_content" />     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="btnClick"         android:layout_gravity="center"         android:id="@+id/btnIntent"         android:text="Go back"         />     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/viewtx"         android:layout_gravity="center"         android:textSize="25dp"         /> </LinearLayout>

For our MainActivity.java, we want to maintain the state of the activity on intent to the second activity. We will enter text in the edit text widget and click view text to print out the text to our TextView. Then click the intent to activity2 to navigate to the second activity.

Our MainActivity.java code 


package info.whats_online.maintainactivitystate; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity {     private EditText eTxt;     private TextView vTxt;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         eTxt=(EditText) findViewById(R.id.edtx);         vTxt=(TextView) findViewById(R.id.viewtx);     }     public void btnClick(View v) {         int id = v.getId();         switch (id) {             case R.id.btnView:                 vTxt.setText(eTxt.getText());                 break;             case R.id.btnIntent:                 if(!eTxt.getText().toString().trim().equals("")){                     Intent intent = new Intent(MainActivity.this, Activity2.class);                     startActivityForResult(intent, 1);                 }else{                     Toast.makeText(getApplicationContext(),"Enter text", Toast.LENGTH_SHORT).show();                 }                 break;         }     }         public void onActivityResult(int requestCode, int resultCode, Intent data) {             super.onActivityResult(requestCode, resultCode, data);             if (requestCode == 1) {                 if(resultCode == RESULT_OK){                     String text=data.getStringExtra("text");                     vTxt.setText("Name 1: "+eTxt.getText()+ "\n Name 2: "+text);                 }             }         } }


MainActivity.java contains the code for navigating to the second activity and onActivityResult() function. We will use startActivityForResult(intent, 1) function to wait for callbacks when the started activity decided to finish, i.e.,  This function expects to get some data from newly started child activity by startAcitvityForResult() and return that to parent activity.

For the onActivityResult() Function receives the results started by the startAcitvityForResult() function as shown above.

Activity2.java code


package info.whats_online.maintainactivitystate; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class Activity2 extends Activity {     EditText eTxt;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity2);     eTxt=(EditText) findViewById(R.id.edtx);     }     public void btnClick(View v){        if(!eTxt.getText().toString().trim().equals("")){            Intent intent=new Intent(Activity2.this, MainActivity.class);            Bundle extra=new Bundle();            extra.putString("text", eTxt.getText().toString());            intent.putExtras(extra);            setResult(RESULT_OK, intent);            finish();        }else{            Toast.makeText(getApplicationContext(), "Enter text", Toast.LENGTH_SHORT).show();        }     } }

For our second activity, enter the text in our edit text and click the 'Go back' to go to our previous activity. Then we will use the bundle class to pass multiple values to the previous class and setResult() function to pass the data to our previous activity.


Thanks for your time. Subscribe to our newsletter to get the latest updates directly to your mailbox.

Download Source Code











You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment