Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:17:05+00:00 2026-06-14T01:17:05+00:00

I have a main activity with a ListView containing a list of names. On

  • 0

I have a main activity with a ListView containing a list of names. On click of the list item, 2nd activity will be fired. On pressing BACK button on the 2nd activity, I should come back to the 1st activity with the same state as that, at the time of firing second activity. may I know how could I achieve this..?

Please attach some sample code.

here is my sample code

Countries_List_Activity.java




public class Countries_list_Activity extends Activity 
{
     String[] countries_list = new String[] {
             "India",
             "Pakistan",
             "Sri Lanka",
             "China",
             "Bangladesh",
             "Nepal",
             "Afghanistan",
             "North Korea",
             "South Korea",
             "Japan",
             "Australia",
             "GreenLand",
             "Las vegas",
             "U.K",
             "Canada",
             "Zimbabwe",
             "Netherland",
             "Singapore",
             "Dubai",
             "Burma",
             "Sutherland",
             "Weat Indies",
             "New Zealand",
             "Kenya",
             "Namibia"
     };

     int index = 0;
     ListView list;
     Context context=this;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         list = (ListView) findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, countries_list);

        list.setAdapter(adapter);


        list.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {
                Object tmp = parent.getItemAtPosition(position);
                String name=tmp.toString();
                System.out.println(name);

                Intent myIntent = new Intent();
                //myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                myIntent.setClassName("com.mink7.countries","com.mink7.countries.Country_Selected_Activity");
                myIntent.putExtra("name", name);
                startActivity(myIntent);
            }
        });


    }


    protected void onPause()
    {
        super.onPause();

        index = list.getFirstVisiblePosition();
        // Save scroll position
       /* SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
        SharedPreferences.Editor editor = preferences.edit();
        int scroll = list.getScrollY();
        editor.putInt("ScrollValue", scroll);
        editor.commit();*/
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        list.setSelectionFromTop(index, 0);
        // Get the scroll position
        /*SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
        int scroll = preferences.getInt("ScrollView", 0);
        list.scrollTo(0, scroll);*/
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Country_selected_Activity.java



package com.mink7.countries;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Country_Selected_Activity extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selected_country_display);
        TextView tv=(TextView) findViewById(R.id.textView1);
        Button b=(Button) findViewById(R.id.button1);

        Bundle extras = getIntent().getExtras();

        String name =extras.getString("name");
        tv.setText(name);

        b.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v)
            {
                Intent intent=new Intent();
                intent.setClassName("com.mink7.countries","com.mink7.countries.Countries_list_Activity");
                startActivity(intent);

            }
        });


    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:17:07+00:00Added an answer on June 14, 2026 at 1:17 am

    There are many ways to do that. one simple solution is

    Make your index static variable;

      public static int index = 0;
    

    add this in

    onItemClick(AdapterView<?> parent, View view,int position, long id)
    
     index = position;
    

    the below line in onPuase

    //index = list.getFirstVisiblePosition();
    

    do this in onResume()

     list.setSelection(index);
    
    //list.setSelectionFromTop(index, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main menu activity with a bunch of buttons. One button starts
I have a lot of buttons on main activity. Each button run this same
I have the following problem. My main activity consists of a ListView populated with
I have a listview in my main activity which shows the detail info about
Have a problem. In my main Activity I have a ListView. And I need
I have a ListView on main activity, which shows a Contact picture on the
I have a listview withcin a activity and when someone clicks on an item
I have an array that successfully shows as a listview as the main activity.
i have made menu list containing play, settings, exit. but clicking the button doesn
I have following goal: From a list in main activity that extends ListActivity, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.