I want to use putExtras
with if statement in getExtras. If in first activity I click an image, it should perform an action, but when I click other image in first activity, it should perform another action.
Here is my code, code works but know not how to differentiate between actions in next activity:
public class wuu extends AppCompatActivity {
TextView t1, t2, t3, t4, t5, t6, t7, t8, t9;
frix_wuxu aa = new frix_wuxu();
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wuu);
t1 = (TextView) findViewById(R.id.first);
t2 = (TextView) findViewById(R.id.second);
final Bundle extras = new Bundle();
extras.putString("abc", "a");
extras.putString("name", "b");
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(wuzu.this, frix_wuxu.class);
intent.putExtras(extras);
startActivity(intent);
}
});
t2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(wuzu.this, frix_wuxu.class);
intent.putExtras(extras);
startActivity(intent);
}
});
}
}
In next activity, if you want to know which action (based on which button is clicked) then you can put a string into extra, for example
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(wuzu.this, frix_wuxu.class);
intent.putExtras(extras);
intent.putExtra("action", "first");
startActivity(intent);
}
});
t2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(wuzu.this, frix_wuxu.class);
intent.putExtras(extras);
intent.putExtra("action", "second");
startActivity(intent);
}
});
In the next activity
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
String action = getIntent().getStringExtra("action");
if (action.equals("first")) {
Log.i("TAG", "First button is clicked");
} else if (action.equals("second")) {
Log.i("TAG", "Second button is clicked");
}
}
}
You just need to add another value in putExtra
as flag. Usually you can achieve it by using an enum like this:
public Enum Action {
ONE, TWO
}
then add it to your Extra:
final Bundle extras = new Bundle();
extras.putString("abc", "a");
extras.putString("name", "b");
// add the action
extras.putSerializable("action", Action.ONE);
The better ways is by packing the whole of your data and Action
as a pojo like this:
// You can also use Parcelable instead of Serializable.
public class MyData implement Serializable {
private String abc;
private String name;
private Action action;
// constructor
// setter
// getter
}
then you can use it for your Extras:
MyData myData = new MyData();
myData.setAbc("a");
myData.setName("b");
myDaya.setAction(Action.ONE);
final Bundle extras = new Bundle();
extras.putSerializable("myData", myData);
to read it in your next Activiy, you can use this:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
MyData myData = (MyData) bundle.getSerializable("myData");
// then do something by the action:
switch (myData.getAction) {
case ONE:
// do something
break;
case TWO:
// do something
break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.