Pass the data through an Intent from the sending activity/fragment to the receiving activity/fragment

 

Fragment data send Bundle

AdminViewTabTecherRecodingsFragment m= new AdminViewTabTecherRecodingsFragment();
               
Bundle args = new Bundle();
//                ArrayList<MEYE_USER> datasend = new ArrayList<MEYE_USER>();
//               Toast.makeText(context, ""+obj.getImage(), Toast.LENGTH_SHORT).show();
//                data.add(obj);
//                args.putSerializable("id", datasend);
//                m.setArguments(args);
               
Gson gson= new Gson();
               
args.putString("id",gson.toJson(obj));
               
m.setArguments(args);
               
loadFragment(m);

Fragment data Received Bundle object

Bundle b = getArguments();
String objString= b.getString("id");
MEYE_USER obj= new Gson().fromJson(objString,MEYE_USER.class);
Toast.makeText(getContext(), ""+obj.getRole(), Toast.LENGTH_SHORT).show();
if
(obj.getImage()!=null){
    Picasso.get().load(Uri.parse(Api.
BASE_URL+"api/get-user-image/UserImages/"+obj.getRole()+"/"+obj.getImage())).into(Binding.profileImageTeacher);
}

Create Redirect to another intent and share Data

// Redirect to another intent , new page move
           
Intent i=new Intent(MainActivity.this,MainActivity2.class);

// Redirect to another intent and share Data
Bundle bund= new Bundle();
           
bund.putString("user_data","My name");
           
i.putExtras(bund);

// start Activity
           
startActivity(i);


Get Bundle in Activity 

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");


List of String send Intent then Received List Of String

//send data list of String in Activity

ArrayList<String> imageList = new ArrayList<>();
           
Intent intent = new Intent(this, NextActivity.class);
           
intent.putStringArrayListExtra("IMAGE_LIST", imageList);
           
startActivity(intent);

//Receive data using intent.getStringArrayListExtra()...
           
ArrayList<String> imageList = new ArrayList<>();
           
Intent intent = getIntent();
           
imageList = intent.getStringArrayListExtra("IMAGE_LIST");


List of Object send Intent then Received List Of Object

// send object of list

ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",object);
intent.putExtra("BUNDLE",args);
startActivity(intent);
// received object of list
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");

JSON Array send Intent and Receive in JSON Array

JSONArray jsonArray = new JSONArray();
Intent intent = new Intent(getApplicationContext(),AdminSettingAssignCourseAssignStudentActivity.class);
intent.putExtra("Data",jsonArray.toString());
startActivity(intent);
//received Json Array in String 
String IntentData=getIntent().getStringExtra("Data");
// Save JsonArray from String
try {
jsonArrayIntent = jsonArrayIntent = new JSONArray(IntentData);
    JSONObject jsonObjectIntent=jsonArrayIntent .getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}

Send Intent DATA and Receive in String then Covert Object 

//send an object in a String from intent
Intent i =new Intent(getApplicationContext(), StudentDashBoardActivity.class);
i.putExtra("IntentData",new Gson().toJson(user));
startActivity(i);
//receive Object in String
String IntentData= getIntent().getStringExtra("IntentData");
MEYE_USER user= new Gson().fromJson(IntentData,MEYE_USER.class);

example, you can use the following code to pass a String value with the key "title" to the receiving activity

  1. Intent intent = new Intent(this, ReceivingActivity.class); intent.putExtra("title", "Your Title Here"); startActivity(intent);
  2. Received
    String title = getIntent().getStringExtra("title");













Object specific key base get array list

//Object specific key base get array list
JSONObject jsonObjectIntent=null;
jsonObjectIntent  = jsonArrayIntent .getJSONObject(0);
ArrayList<SectionOffer> sectionOffers;
try {
sectionOffers=gson.fromJson(jsonObjectIntent.getString("sectionOfferId"),
new TypeToken<ArrayList<SectionOffer>>(){}.getType());
} catch (JSONException e) {
e.printStackTrace();
}

  gson in String {"data":[]}  then convert to map 

String json = "{\n" +
       
\"data\": [\n" +
       
"    {\n" +
       
"      \"id\": 1,\n" +
       
"      \"ip\": \"192.168.1.1\",\n" +
       
"      \"name\": \"Admin\",\n" +
       
"      \"channel\": \"5\",\n" +
       
"      \"host\": \"Admin\",\n" +
       
"      \"password\": \"Admin\"\n" +
       
"    }\n" +
       
"  ]\n" +
       
"}";
Map <String,ArrayList<DVR>> map;

Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<DVR>>>(){}.getType();
map = gson.fromJson(json, type);
 
ArrayList<DVR> dvrList = map.get("data");
 
System.out.println("IPshow+"+dvrList.get(0).getIp());



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.