Advertisement
kivaari

onactivity result example

Jan 24th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1.     public void selectDestination(View view){
  2.         //add if clause to check if we are already in destination
  3.         Intent intent = new Intent(this, destinationActivity.class);
  4.         intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  5.         intent.putExtra("car_obj", (Parcelable) car);
  6.         startActivityForResult(intent, 1);
  7.     }
  8.  
  9.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  10.         super.onActivityResult(requestCode, resultCode, data);
  11.         if (requestCode == 1) {
  12.             if(resultCode == RESULT_OK){
  13.                 if(data.getParcelableExtra("car_obj") != null)
  14.                     car = (Car) data.getParcelableExtra("car_obj");
  15.             }
  16.         }
  17.     }
  18.  
  19. ///Started Activity///
  20.          Intent intent = getIntent();
  21.         if(intent.getParcelableExtra("car_obj") != null) {
  22.             if (car == null) car = (Car) intent.getParcelableExtra("car_obj");
  23.         }
  24.         else{   //load from file if no intent and no result
  25.             CarFileManager cfm = CarFileManager.getInstance();
  26.             cfm.setFile(getBaseContext());
  27.  
  28.             cfm.loadCar(getBaseContext());
  29.             car = cfm.getCar();
  30.         }
  31.         intent.removeExtra("car_obj");
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.     @Override
  40.     public void onBackPressed(){
  41.          Intent intent = new Intent(this, destinationActivity.class);
  42.         intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  43.         intent.putExtra("car_obj", (Parcelable) car);
  44.         setResult(RESULT_OK, intent);
  45.  
  46.         super.onBackPressed();
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement