0

I have an Activity called searchProcedures which allows a user to select from a listview of medical procedures. I navigate to this activity from two other activities called searchHome and describeVisit. I needed a way for searchProcedures to know which activity it should navigate back to onClick. So I pass an intent.extra from searchHome or describeVisit (key:"sentFrom" value""). Then in searchProcedures I use the following code to determine which class to navigate to.

Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if(!extras.isEmpty()){
        if(extras.containsKey("sentFrom")){
            if(extras.getString("sentFrom") == "searchHome"){
                returnIntent = new Intent(searchProcedures.this, searchHome.class);
            }
            else if(extras.getString("sentFrom") == "describeVisit"){
                returnIntent = new Intent(searchProcedures.this, describeVisit.class);
            }
            else{
                Log.d("failed", "the value of getString is " + extras.getString("sentFrom"));
            }
        }
    }

Checking the Log values, the correct values are being passed to and from activity, but when I check extras.getString("sentFrom") == "searchHome/describeVisit" it comes back as false, and returnIntent remains un-initialized. I have tried putting .toString after the .getString to no avail.

Taylor Barton
  • 45
  • 2
  • 10
  • Show your passing intent code. You need to pass a string value with the key "sentfrom" inorder to obtain it as "getString("")" in the other activity – Sonu Sanjeev Jan 05 '18 at 02:19

2 Answers2

1

1.

== compares the object references, not the content

You should use:

"searchHome".equals(extras.getString("sentFrom"))

Remeber to check blank space,...

2.

You can use a static variable in your SearchProceduresActivity to check where it comes from

SearchProceduresActivity

public static int sFrom = SEARCHHOME;

SearchHomeActivity:

Intent myIntent = new Intent(SearchHomeActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = SEARCHHOME;
startActivity(myIntent);

DescribeVisitActivity:

Intent myIntent = new Intent(DescribeVisitActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = DESCRIBEVISIT;
startActivity(myIntent);

SEARCHHOME, DESCRIBEVISIT value is up to you

Hope this help!

Liar
  • 1,235
  • 1
  • 9
  • 19
0

String compare should use equal not ===

yu wang
  • 283
  • 1
  • 6