我的代码如下所示:
TableActivity.java
public class TableActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_selection);
itemName = (TextView) findViewById(R.id.itemName);
foodMenuLayout = (LinearLayout) findViewById(R.id.foodMenuLayout);
drinkMenuLayout = (LinearLayout) findViewById(R.id.drinksMenuLayout);
tableNo = (TextView) findViewById(R.id.tableNo);
foodMenu = (TextView) findViewById(R.id.foodMenu);
drinksMenu = (TextView) findViewById(R.id.drinksMenu);
tapLayout = (LinearLayout) findViewById(R.id.tapLayout);
foodImage = (ImageView) findViewById(R.id.foodImage);
drinkImage = (ImageView) findViewById(R.id.drinkImage);
getFoodTruckInfo();
tapLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tableList = TableList.getInstance(MyCart.getInstance().getTableNo());
tableList.setItemListener(TableActivity.this);
tableList.setCancelable(false);
tableList.show(getSupportFragmentManager(), null);
}
});
setFontStyle();
foodMenuLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(tableNo.getText().toString().trim())) {
Toast.makeText(TableActivity.this, "Please select table", Toast.LENGTH_SHORT).show();
} else {
lauchItemActivity();
}
}
});
drinkMenuLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(tableNo.getText().toString().trim())) {
Toast.makeText(TableActivity.this, "Please select table", Toast.LENGTH_SHORT).show();
} else {
lauchDrinksActivity();
}
}
});
}
private void lauchItemActivity() {
FoodtruckInfo foodtruckInfo = new FoodtruckInfo();
foodtruckInfo.setFoodTruckName(foodTruck.getFoodTruckName());
foodtruckInfo.setFoodTruckImage(foodTruck.getFoodTruckLogo());
foodtruckInfo.setFoodTruckId(foodTruck.getFoodTruckId());
foodtruckInfo.setOrderStatus(foodTruck.getFoodTruckOpenStatus());
Intent intent = new Intent(TableActivity.this, FoodTruckItemsActivity.class);
intent.putExtra(Consts.FOODTRUCK_INFO, foodtruckInfo);
intent.putExtra(Consts.IS_SEARCH, false);
intent.putExtra(Consts.IS_DRINKS, false);
intent.putParcelableArrayListExtra(Consts.FOODTRUCK_ITEM, (ArrayList<? extends Parcelable>) foodTruck.getFoodTruckItemList());
startActivityForResult(intent, Consts.ITEMS_ACTIVITY);
}
private void lauchDrinksActivity() {
FoodtruckInfo foodtruckInfo = new FoodtruckInfo();
foodtruckInfo.setFoodTruckName(foodTruck.getFoodTruckName());
foodtruckInfo.setFoodTruckImage(foodTruck.getFoodTruckLogo());
foodtruckInfo.setFoodTruckId(foodTruck.getFoodTruckId());
foodtruckInfo.setOrderStatus(foodTruck.getFoodTruckOpenStatus());
Intent intent = new Intent(TableActivity.this, FoodTruckDrinksActivity.class);
intent.putExtra(Consts.FOODTRUCK_INFO, foodtruckInfo);
intent.putExtra(Consts.IS_SEARCH, false);
intent.putExtra(Consts.IS_DRINKS, true);
intent.putParcelableArrayListExtra(Consts.FOODTRUCK_ITEM, (ArrayList<? extends Parcelable>) foodTruck.getFoodTruckDrinkList());
startActivityForResult(intent, Consts.DRINKS_ACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == Consts.ITEMS_ACTIVITY) {
lauchDrinksActivity();
} else if (requestCode == Consts.DRINKS_ACTIVITY) {
lauchItemActivity();
}
}
}
}
FoodTruckItemsActivity.java
public class FoodTruckItemsActivity extends AppCompatActivity implements
FoodTruckItemsAdapter.OnItemClickListener, FoodTruckItemFragment.FoodTruckItemListener,
ViewPager.OnPageChangeListener, View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_truck_item);
routeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
routeToAnotherMenu();
}
});
}
private void routeToAnotherMenu() {
setResult(Activity.RESULT_OK);
}
}
FoodTruckItemsActivity.java 和 FoodTruckDrinksActivity.java 完全相同。所以我想要的是,如果我按routeView
,它应该从FoodTruckItemsActivity.java
切换到FoodTruckDrinksActivity.java
,反之亦然。
为此我写了routeToAnotherMenu()
方法。现在的问题是如果我不在finish()
内写routeToAnotherMenu
,它就不会加载另一个活动。但如果我在setResult()
之后写完,那么它就能完美地运作。
我不喜欢在该方法中使用finish()的主要原因是因为它会擦除数据。那么有没有办法可以在不编写finish()方法的情况下完成它?
答案 0 :(得分:1)
应该在开始另一个活动之前使用setResult(),或者如果您希望启动此活动的活动在退出后调用结果(调用完成)。Here is a link to docs
我可以想象两种方法可以达到你想要达到的效果:
1)在完成()之后(例如,从当前活动中)分离想要保留的“数据”。 如果FoodTruckItemsActivity.java和FoodTruckDrinksActivity.java几乎完全相同,那么它们应该从一个共同的父级继承,这样你就可以轻松完成工作。 它也更好,因为“TableActivity”每次都会启动一个新的意图,所以没有调用结束是没用的。
2)您还可以更改将FoodTruckItemsActivity和FoodTruckDrinksActivity转换为Fragments的方法,将它们交换为“TableActivity”的片段容器,并将数据共享到活动中。
答案 1 :(得分:1)
答案 2 :(得分:1)
在您的情况下, FoodTruckItemActivity 是您打开 FoodTruckDrinkActivity 的常用活动。
现在,For E.g声明TableActivity中的ArrayList将所有选定的数据存储在该arrayList中。
现在,当您为FoodTruckItemsActivity启动ActivityForResult()时。在FoodTruckItemsActivity中声明ArrayList以添加数据。现在当你路由到另一个菜单时写下这个。
Intent intent=new Intent();
intent.putStringArrayList("FOODITEMLIST", myItemList);
setResult(intent);
finish();
现在在您的Table活动中,结果出现在onActivityResult中。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == Consts.ITEMS_ACTIVITY) {
// Get the arraylist and add that list in Table Activity Main ArrayList
// which we declare earlier in Table Activity.
lauchDrinksActivity();
} else if (requestCode == Consts.DRINKS_ACTIVITY) {
lauchItemActivity();
}
}
}