Reputation: 4517
My code is as shown below:
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 and FoodTruckDrinksActivity.java are identical. So what I want here is if I press routeView
, it should switch from FoodTruckItemsActivity.java
to FoodTruckDrinksActivity.java
and vice-versa.
For that I have written routeToAnotherMenu()
method. Now problem here is if I do not write finish()
inside routeToAnotherMenu
, it does not load another activity. But if I write finish after setResult()
then it works perfectly.
The main reason why I do not prefer finish() inside that method because it will erase the data . So is there any way through which I can do it without writing finish() method?
Upvotes: 0
Views: 153
Reputation: 3101
In Your case FoodTruckItemActivity is Common activity from where you open your FoodTruckDrinkActivity.
Now, For E.g declare the ArrayList in TableActivity to store all your selected data in that arrayList.
Now When you startActivityForResult() for FoodTruckItemsActivity. In FoodTruckItemsActivity declare the ArrayList for adding the data. Now when you route to another menu write this.
Intent intent=new Intent();
intent.putStringArrayList("FOODITEMLIST", myItemList);
setResult(intent);
finish();
Now In your Table activity the result comes here in 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();
}
}
}
Upvotes: 1
Reputation: 3131
From the docs for onActivityResult()
:
Called when an activity you launched exits (...)
Just setting the result won't cause the method to be called.
If you want to switch freely between screens, maybe you should consider using Fragments?
Upvotes: 1
Reputation: 1113
The setResult() should be used before starting another activity, or if you want the activity that launched this activity to collect the result after you exit (calling finish).Here is a link to docs
I could imagine 2 ways for the result you want to achieve:
1)Decouple the "data" you want to mantain after the finish() (for example, into a singleton) from the current activity. If FoodTruckItemsActivity.java and FoodTruckDrinksActivity.java are almost identical, they should inherite from a common parent, so you will ease the job. It's better also because "TableActivity" starts always a new intent every time, so it is useless to not call the finish.
2)You could also change approach converting FoodTruckItemsActivity and FoodTruckDrinksActivity to Fragments, swapping them into a fragment container of "TableActivity" and sharing the data into the activity.
Upvotes: 1