Go to Main activity without Reloading or Refreshing activity

In this article I am going to discuss about to back main activity without refresh or reload activity.

If you have multiple activities as well as long code and you want to go main activity then I am telling you tips to back main activity and save your code as well as efficiency of the application. This code save your time, code as well as efficiency of your android application

MainAcitivity =>  Second Activity  => ThirdActivity   =>  MainAcitivity

Follow the steps which are given below -:

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

 

Now add the following control in res/layout/activity_main.xml


<RelativeLayout 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: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" >
 
    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="34dp"
        android:ems="10" />
 
    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etName"
        android:layout_below="@+id/etName"
        android:layout_marginRight="57dp"
        android:text="Next" />
 
    <TextView
        android:id="@+id/tvMainActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Main Activity" />
 
</RelativeLayout> 

In res/layout add the two xml file name should be second.xml and third.xml .

In second.xml write the following code

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/tvSecondActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Second Activity" />
 
    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="39dp"
        android:layout_marginTop="30dp"
        android:text="Next" />
 
</RelativeLayout> 

In third.xml write the following code

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/btnBackMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="108dp"
        android:text="Back in Main" />
 
    <TextView
        android:id="@+id/tvThird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btnBackMain"
        android:text="Third Activity" />
 
</RelativeLayout> 

In src/ com.example.backmainactivity add the two new class name should

be SecondActivity.java and ThirdActivity.java

Write the following code in MainActivity.java

package com.example.backmainactivity;

 
public class MainActivity extends Activity {
      Button btnNext;
      EditText etname;
 
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnNext=(Button)findViewById(R.id.btnNext);
            etname=(EditText)findViewById(R.id.etName);
            btnNext.setOnClickListener(new View.OnClickListener() {  
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
//Create a new Activity and start the new activity
                        Intent intent=newIntent(MainActivity.this,SecondActivity.class);
                        startActivity(intent);  
                  }
            });  
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
      }
 

In SeondActivity.java write the following code

package com.example.backmainactivity;

 
public class SecondActivity extends Activity {
Button btnNext;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            btnNext=(Button)findViewById(R.id.btnNext);
            btnNext.setOnClickListener(new View.OnClickListener() {
 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent=newIntent(SecondActivity.this,ThirdActivity.class);
                        startActivity(intent);
 
                  }
            });
      }
 

Now last paste the following code in Thirdactivity.java

package com.example.backmainactivity;

 
public class ThirdActivity extends Activity {
 
      Button btnBackMain;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third);
            btnBackMain=(Button)findViewById(R.id.btnBackMain);
            btnBackMain.setOnClickListener(new View.OnClickListener() {
 
                  @SuppressLint("NewApi")
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
// TODO getIntent() return the first intent. And navigateUpTo method is use to back the source activity
      navigateUpTo(getIntent());//Back in First Activity  
                  }
            });  
      }
 

Now define the activity in AndroidManifest.xml


   </activity>

        <activityandroid:name="com.example.backmainactivity.SecondActivity"/>
        <activityandroid:name="com.example.backmainactivity.ThirdActivity"/> 

Run your application output like this

Go to Main activity without Reloading or Refreshing activity

Click the Next button and a new activity will be open

Go to Main activity without Reloading or Refreshing activity

Again click Next button

Go to Main activity without Reloading or Refreshing activity

Click  Back in Main button and go back in main activity

Go to Main activity without Reloading or Refreshing activity

 

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

 And suggestion to improve this article in the comment box.

  Modified On Nov-30-2017 05:41:33 AM

Leave Comment