D
D
Dauren S2016-10-05 10:14:09
Java
Dauren S, 2016-10-05 10:14:09

Why is if not defined?

Passed fragment value

@Override public void onItemClick(View view, int position) {
    // do whatever
    usr  = adapter.getItem(position);
    Context context=view.getContext();
    Intent intent = new Intent(context, DetailActivity.class);
    //intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, (Parcelable) usr);
    intent.putExtra("fragment", "OneFragment");
    context.startActivity(intent);
}

In DetailActivity I get
String fragment= getIntent().getStringExtra("fragment");
if(fragment== "OneFragment")
{
    Toast toast2 = Toast.makeText(this, "hello ddd", Toast.LENGTH_SHORT);
    toast2.show();
}

But if doesn't define Why??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2016-10-05
@dauren101

For reference types, == tests for reference equality, not value equality. Use the .equals() method :
if(fragment.equals("OneFragment")) {}

S
sirs, 2016-10-05
@sirs

Что значит не определяется?
В java строки сравнивают по equals (если они не были добавлены через .intern()), а у Вас написано if(fragment== "OneFragment").
Попробуйте заменить на
if("OneFragment".equals(fragment)) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question