How to Go Back to Previous Activity Android

Go back previous activity without Reloading or Refreshing activity in Android

In this article, I am going to demonstrate how to go back to previous activity without reloading and refreshing the activity every time.

When we use Intent class and start activity method to back previous activity then our application refreshed again as well as takes time to reload.

To use the start activity method you should know the name of previous activity. For all these reasons you can follow my steps which steps will be saved your code and time, as well as your application, will be run fast.

Follow the steps which are given below -:

  • Open Eclipse
  • Select New Android Application Project
  • Enter Application Name as BackNoRefresh
  • Click Next
  • Click Finish.

Now go to the res/layout/activity_main.xml and paste the following code

          <RelativeLayoutxmlns:android='http://schemas.android.com/apk/res/android'                      
    xmlns:tools='http://schemas.android.com/tools'
    style='@style/Container'
    android:layout_width='match_parent'
    android:layout_height='match_parent'
    android:layout_gravity='center'
    android:paddingBottom='@dimen/activity_vertical_margin'
    android:paddingLeft='@dimen/activity_horizontal_margin'
   android:paddingRight='@dimen/activity_horizontal_margin'
    android:paddingTop='@dimen/activity_vertical_margin'
    tools:context='.MainActivity' >
    <TextView
        android:id='@+id/tvEName'
        android:layout_marginTop='80dp'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:layout_alignParentTop='true'
        android:text='@string/EnterName'
        android:textColor='#000'
        android:textSize='14sp' />
    <EditText
        android:id='@+id/etUID'
        android:layout_width='240dp'
        android:layout_height='35dp'
        android:layout_alignParentLeft='true'
        android:layout_alignRight='@+id/etEname'
        android:layout_below='@+id/tvEName'
        android:ems='10'
        android:hint='@string/EName'
        android:maxLength='20'
        android:textSize='14sp' />
    <requestFocus />
    <TextView>
        android:id='@+id/tvUIDName'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:layout_alignParentLeft='true'
        android:layout_below='@+id/etUID'
        android:layout_marginTop='24dp'
        android:text='@string/Enter_name'
        android:textColor='#000'
        android:textSize='14sp' />
    <EditText
        android:id='@+id/etEname'
        android:layout_width='240dp'
        android:layout_height='35dp'
        android:layout_alignParentLeft='true'
        android:layout_below='@+id/tvUIDName'
        android:ems='10'
        android:hint='@string/UserName'
        android:maxLength='15'
        android:textSize='14sp' />
    <TextView
        android:id='@+id/tvState'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:layout_below='@+id/etEname'
        android:layout_marginTop='20dp'
        android:text='@string/Sel_State'
        android:textColor='#000'
        android:textSize='14sp' />
    <Spinner
        android:id='@+id/spState'
        android:layout_width='240dp'
        android:layout_height='44dp'
        android:layout_below='@+id/tvState' />

    <Button
        android:id='@+id/btnSubmit'
        android:layout_marginLeft='80dp'
        android:layout_width='70sp'
        android:layout_height='40sp'
        android:layout_below='@+id/spState'
        android:layout_marginTop='19dp'
        android:layout_toRightOf='@+id/tvState'
        android:text='@string/Submt' />
    <TextView
        android:id='@+id/tvProfile'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:layout_alignParentTop='true'
        android:text='User Profile'
        android:textColor='#000'
        android:textSize='17sp' />
    <TextView
        android:id='@+id/tvPersonalInfo'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:layout_below='@+id/tvProfile'
        android:layout_marginTop='20dp'
        android:text='Personal Information*'
        android:textColor='#000' />
</RelativeLayout>

And add a two new layout XML file name should be second.xml and third.xml

 In second.xml add the TextView and EditText to bind the user information which is coming from Main Activity.

In second.xml
          <?xml version='1.0' encoding='utf-8'?>                      
<LinearLayoutxmlns:android='http://schemas.android.com/apk/res/android'
    style='@style/Container'
    android:layout_width='match_parent'
    android:layout_height='match_parent'
    android:orientation='vertical' >
    <TextView
        android:id='@+id/tvHead'
        android:layout_width='210dp'
        android:layout_height='wrap_content'
        android:layout_marginRight='25dp'
        android:text='@string/welcome'
        android:textSize='18sp' />
    <LinearLayout
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:orientation='vertical' >
        <TextView
            android:id='@+id/tvContact'
            android:layout_width='wrap_content'
            android:layout_height='wrap_content'
            android:layout_marginTop='30sp'
            android:text='Contact Information*'
            android:textColor='#000' />
        <TextView
            android:id='@+id/tvContact'
            android:layout_width='wrap_content'
            android:layout_height='wrap_content'
            android:layout_marginTop='30sp'
            android:text='Contact Number'
            android:textColor='#000' />
        <EditText
            android:id='@+id/etContact'
            android:layout_width='270dp'
            android:layout_height='40dp'
            android:ems='10'
            android:hint='Contact'
            android:inputType='number' />
        <TextView
            android:id='@+id/tvAddress'
            android:layout_width='wrap_content'
            android:layout_height='wrap_content'
            android:text='Address'
            android:textColor='#000' />
        <EditText
            android:id='@+id/etAddress'
            android:layout_width='270dp'
            android:layout_height='40dp'
            android:ems='10'
            android:hint='Address' />
    </LinearLayout>
    <LinearLayout
        android:layout_width='match_parent'
        android:layout_height='wrap_content' >
        <Button
            android:id='@+id/btnBack'
            android:layout_width='72dp'
            android:layout_height='40dp'
            android:layout_marginLeft='110dp'
            android:layout_marginTop='15dp'
            android:onClick='Back'
            android:text='@string/Back' />
        <Button
            android:id='@+id/btnNext'
            android:layout_width='72dp'
            android:layout_height='40dp'
            android:layout_marginLeft='10dp'
            android:layout_marginTop='15dp'
            android:onClick='GetNext'
            android:text='Next' />
    </LinearLayout>
</LinearLayout>
And third.xml
          <?xml version='1.0' encoding='utf-8'?>                      
<LinearLayoutxmlns:android='http://schemas.android.com/apk/res/android'
    style='@style/Container'
    android:layout_width='match_parent'
    android:layout_height='match_parent'
    android:orientation='vertical' >
    <TextView
        android:id='@+id/tvWelcome'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:text='Welcome'
        android:textColor='#000' />
    <TextView
        android:layout_marginTop='25dp'
        android:id='@+id/tvOtherInfo'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:text='Other Information *'
        android:textColor='#000' />
    <TextView
        android:layout_marginTop='30dp'
        android:id='@+id/textView3'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:text='Marital Status-:'
        android:textColor='#000' />
    <Spinner
        android:id='@+id/spinCheckMarried'
        android:layout_width='match_parent'
        android:layout_height='wrap_content' />
    <TextView
        android:id='@+id/tvNationality'
        android:layout_width='wrap_content'
        android:layout_height='wrap_content'
        android:text='Nationality'
        android:textColor='#000' />

    <EditText
        android:id='@+id/etNationality'
        android:hint='Nationality'
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:ems='10' >
        <requestFocus />
    </EditText>
    <LinearLayout         android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:orientation='horizontal' >
        <Button
            android:layout_marginLeft='39dp'
            android:id='@+id/btnBack'
            android:layout_width='75dp'
            android:layout_height='50dp'
            android:text='Back' />
    <Button
          android:layout_marginLeft='39dp'
        android:id='@+id/btnFinish'
        android:layout_width='75dp'
        android:layout_height='50dp'
        android:text='Finish' />
    </LinearLayout>
</LinearLayout>

Define the activity in Andr oidManifest.xml

          <activity android:name='com.example.backnorefresh.Second'                                  android:theme='@android:style/Theme.NoTitleBar'/>                      
        <activity android:name='com.example.backnorefresh.Third'
              android:theme='@android:style/Theme.NoTitleBar'/>
    </application>

Now write the following code in MainActivity.java

In MainActivity.java send the User Name to another activity by using Intent class.

package com.example.backnorefresh;          
public class MainActivity extends Activity {
                TextView tvProfile;
                EditText etUname;
                EditText etUid;
                EditText etContact;
                ListView lvState;
                Spinner spState;
                Button btnSubmit;
// ArrayAdapter is defined for set the value Country list in spinner control
                ArrayAdapter<String> adapter;
                ArrayList<String> stateList = new ArrayList<String>();
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                  // TODO Auto-generated method stub
                  super.onCreate(savedInstanceState);
                  super.onResume();
                  setContentView(R.layout.activity_main);
                  btnSubmit = (Button) findViewById(R.id.btnSubmit);
                  etUname = (EditText) findViewById(R.id.etUID);
                  etUid = (EditText) findViewById(R.id.etEname);
                  tvProfile = (TextView) findViewById(R.id.tvProfile);
//Set the country name in statelist which ArrayList, defined on the top
                 stateList.add('Africa');
                 stateList.add('Australia');
                 stateList.add('Belgium');
                 stateList.add('Brazil');
                 stateList.add('Canada');
                 stateList.add('China');
                 stateList.add('Europe');
                stateList.add('France');
                 stateList.add('Greenland');
                 stateList.add('Iceland');
                 stateList.add('India');
                 stateList.add('Japan');
                 stateList.add('Maxico');
                 stateList.add('New Zealand');
                 stateList.add('South Africa');
                 stateList.add('Turkey');
                 stateList.add('United States');
                 spState = (Spinner) findViewById(R.id.spState);

// In Array adapter set the statelist and In Spinner control set the ArrayAdapter

          adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, stateList);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spState.setAdapter(adapter);
                    tvProfile.setTextColor(Color.RED);
                    btnSubmit.setOnClickListener(new View.OnClickListener()                         {
                           @Override
                                  public void onClick(View v)                                         {
                                          // TODO Auto-generated method stub
                                             new Thread(new Runnable()                                                 {
                                             @Override
                                            public void run()                                                 {
                                              // TODO Auto-generated method stub
                                                 try                                                     {
Intent intent = new Intent(MainActivity.this, Second.class);
                                                     String sUname = etUname.getText().toString();
                                            // Insert the value in Intent which is forward in another activity
                                                           intent.putExtra('user', sUname);
                                                           startActivity(intent);
                                                    }                                                 catch (Exception ex)                                                         {
                                                            Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
                                                        }
                                                   }
                                           }).start();
                                 }
                    });
     }
}}

And write the following code in Second.java

          package com.example.backnorefresh;                      
@SuppressLint('ResourceAsColor')
public class Second extends Activity {
      TextView tvHead, tvUname, tvUid, tvContact, tvState;
      Button btnBack, btnNext;
      EditText etuser;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            try {
                  tvHead = (TextView) findViewById(R.id.tvHead);
                  tvHead.setTextColor(Color.RED);
                  String sUser = (String) getIntent().getExtras().get('user');
                  tvHead.setText('Welcome -: ' + sUser);
            } catch (Exception ex) {
                  Toast.makeText(getApplicationContext(), ex.toString(),
                              Toast.LENGTH_LONG).show();
            }
      }

      @Override
      public void onBackPressed() {
            // Write your code here
            super.onBackPressed();
      }
      public void Back(View v) {
            onBackPressed();

      }
      public void GetNext(View view) {
            try {
                  Intent intent = new Intent(Second.this, Third.class);
                  String sName = (String) getIntent().getExtras().get('user');
                  intent.putExtra('name', sName);
                  startActivity(intent);
            } catch (Exception ex) {
                  Toast.makeText(getApplicationContext(), ex.toString(),
                              Toast.LENGTH_LONG).show();
            }
      }
}

And now write the following code in Third.java

          package com.example.backnorefresh;                      
public class Third extends Activity {
                TextView tvUserName,tvFname,tvMname;
      Button btnBack; Button btnBack;
                                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                // TODO Auto-generated method stub
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.third);
            tvUserName = (TextView) findViewById(R.id.tvWelcomeUser);
            tvFname=(TextView)findViewById(R.id.tvFname);
            tvMname=(TextView)findViewById(R.id.tvMname);
            btnBack = (Button) findViewById(R.id.btnBack);
            String sUserName = (String) getIntent().getExtras().get('Uname');
            String sFName = (String) getIntent().getExtras().get('Fathername');
            String sMName = (String) getIntent().getExtras().get('Mothername');
            tvUserName.setTextColor(Color.RED);
            tvUserName.setTextSize(20);
            tvUserName.setText('Welcome -: ' + sUserName);
            tvFname.setText('Father Name-: '+sFName);
            tvMname.setText('Mother Name-: '+sMName); btnBack.setOnClickListener(new View.OnClickListener() {
                //This code will be back in previous activity @Override
                   public void onClick(View v) {
try {
onBackPressed();
                             } catch (Exception ex) {
                                  Toast.makeText(getApplicationContext(), ex.toString(),
                                   Toast.LENGTH_LONG).show();
                                } }
}); }
                @Override
                public void onBackPressed() {
               // Write your code here super.onBackPressed();
                }
}

Now run your Application, output like this

Go back previous activity without Reloading or Refreshing activity in Android

Enter the value in form fields (Name, User Name, and Country). Click n Next button.

Go back previous activity without Reloading or Refreshing activity in Android

It will open a new activity

Go back previous activity without Reloading or Refreshing activity in Android

Fill your Contact Number and Address or click back button.

After click back button, back the previous activity without refresh or reload

Go back previous activity without Reloading or Refreshing activity in Android

If you are fill your Father Name and Mother name then click Next button

Go back previous activity without Reloading or Refreshing activity in Android

Now a new activity will be open

Go back previous activity without Reloading or Refreshing activity in Android

In Back button click the previous activity will be open without refreshing and

reloading.

Go back previous activity without Reloading or Refreshing activity in Android

If Fill your marital status and Nationality then click finish

Go back previous activity without Reloading or Refreshing activity in Android

If you click Finish button then you will get a message and new refresh page will be

open

Go back previous activity without Reloading or Refreshing activity in Android

Thanks for reading this article. You can enter your valuable comments and

suggestion to improve this article in the comment box.

 You should also read this Article - https://www.mindstick.com/Articles/1799/simple-registration-form-using-java-swing

Last updated:7/10/2020 10:54:02 PM

How to Go Back to Previous Activity Android

Source: https://www.mindstick.com/articles/1441/go-back-previous-activity-without-reloading-or-refreshing-activity-in-android

0 Response to "How to Go Back to Previous Activity Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel