samz22
samz22

Reputation: 101

Goto Home Screen when a action bar button is clicked

I have a Image button in my app for every activity.I need to goto home screen when that image button is pressed.I am using the code

btnLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Intent intent=new Intent(SpecificCategoryActivity.this,AreaSelectionActivity.class);

                //startActivity(intent);
                Intent homeIntent = new Intent(SpecificCategoryActivity.this, AreaSelectionActivity.class);
                homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);

            }
        });

But, when I navigate from home screen after clicking on image button it should go to same page from where it had called.

For example,I have Activity1,Activity2,Activity3,Activity4 etc... Activity1 is home page,I will be selecting country and go to Activity2 and then to Activity3 and so on,but if I click Location button from Activity4 it should go to home page and after changing the country it has to got back to Activity4.And one more thing is if country is already selected App will be getting launched from Activity2.

My Activity1 code is

btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if(!autoComplete.getText().toString().isEmpty()) {

                if (isSelectedText) {


                    Intent intent = new Intent(CountrySelectionActivity.this, CategoryListActvity.class);
                     startActivity(intent);


                }
                else{
                    editor.clear();
                    editor.commit(); // commit changes
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CountrySelectionActivity.this);

                    alertDialogBuilder.setMessage("Please specify the correct country name");

                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();

                }
            }


        }
    });

Kindly help in solving the issue. Thanks in advance.

Upvotes: 0

Views: 116

Answers (2)

priya raj
priya raj

Reputation: 362

I will give some pseudo code here
    In Activity4, locationbutton onclick listener will have the below code
    locationbutton .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

       //save a variable to indicate that activity4 is reached
    savePreferences("**pagenavigated**", "4", context);  //need to write //savePreferences

     Intent intent = new Intent(activity4.this, homepage.class);
                         startActivity(intent);

        });
    //We can write this method commonly so all activity can access this
    public static void savePreferences(String key, String value, Context context) {

            SharedPreferences SharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = SharedPreferences.edit();
            editor.putString(key, value);
            editor.commit();
        }
    public static String loadPreferences(Context context, String key) {
            SharedPreferences SharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(context);
            String userDetails = SharedPreferences.getString(key, "");
            return userDetails;

        }
public static void removePreference(String key, Context context) {

        SharedPreferences SharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = SharedPreferences.edit();
        editor.remove(key);
        editor.commit();

    }

    public static void removeAllPreferences(Context context) {

        SharedPreferences SharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = SharedPreferences.edit();
        editor.clear();
        editor.commit();

    }
//In Activity 1, check for sharedpreference variable value and if it is 4, it //should get navigate to 4th screen . To make this happen , after checking //the value, we need to remove the shared value as well
//Activity 1
//In **country dropdown change listener**, check for the value of //**pagenavigated** and if it is 4, start the activity to go for activity 4. //Before that remove the value by using removeAllPreferences method
//So changes will be in onItemSelected
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String navigatedPage = loadPreferences(context,"pagenavigated");
if (navigatedPage.equalsIgnoreCase("4") {
removeAllPreferences(this);  //Remove the preference and go to activity 4)
Intent intent = new Intent(homepage.this, activity4.class);
                         startActivity(intent);
}



Hope that helps!!!

Upvotes: 1

Mayur_Thakur
Mayur_Thakur

Reputation: 661

Here you can use startActivityForResult(Intent intent, int resultCode);

e.g

 Intent intent = new Intent(CountrySelectionActivity.this, CategoryListActvity.class);
    intent.putExtra("Select_Location", true);
    startActivityForResult(intent,007);

Now in your 1st Activity check for the intent data and if you are getting "Select_Location" flag true then after choosing city, call finish();

e.g.

in onCreate method of Activity 1

Intent i=getIntent(); //**i** will be null if Activity is called from another Activity

if(i!=null)
    boolean flag=i.getBooleanExtra("Select_Location", false);
    //now you have flag which indicates that whether this intent is from 4th activity or not.

Let user select the city and in your onClick method just check that our flag is true or not.

if it is true then just use intent and attach all the required info with it and call finish.

e.g

Intent resultIntent = new Intent();
resultIntent.putExtra("city_id", <city id>);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();

Now you need to override the method called onActivityResult(in Activity 4)

e.g.

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK && requestCode == 007 && data != null){
       // take out all the parameters from data(which is an object of Intent).

            }
    }

Upvotes: 0

Related Questions