My login activity needs LoginSuccess where my fragments are loaded there.
LoginActivity.java
:
Intent intent = new Intent(LoginActivity.this,LoginSuccess.class);
Bundle bundle = new Bundle()
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));
intent.putExtras(bundle);
startActivity(intent);
How can I fetch the bundle "appointments" in a fragment? Thanks in advance.
AppointmentsFragment.java
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreate(savedInstanceState);
Bundle bundle = getActivity().getIntent().getExtras();
View v = inflater.inflate(R.layout.fragment_appointments, container, false);
String [] datasource = {"appointments", bundle.getString("appointments")};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout, R.id.txtitem, datasource);
setListAdapter(adapter);
setRetainInstance(true);
Log.d("appointments", bundle.getString("appointments"));
View appointment_date = v.findViewById(R.id.appointment_date);
((TextView)appointment_date).setText(bundle.getString("appointment_date"));
return v;
}
Here's the code for LoginSuccess.java
`public class LoginSuccess extends AppCompatActivity { Toolbar toolbar; TabLayout tabLayout; ViewPager viewPager; ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_success);
toolbar = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new ProfileFragment(), "Profile");
viewPagerAdapter.addFragments(new AppointmentsFragment(), "Appointments");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}`
First open the Fragment
by doing this inside your LoginActivity
LoginActivity.class
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import com.example.vostro.myapplication.fragments.FragmentDemo;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_main);
Bundle bundle = new Bundle();
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));
FragmentDemo fragment = new FragmentDemo();
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.navigation_container, fragment);
fragmentTransaction.commit();
}
}
Then inside your new Fragment get the data by doing this
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.vostro.myapplication.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class FragmentDemo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_demo, null);
String jsonArrayAppointments = getArguments().getString("appointments");
try {
JSONArray jsonArray = new JSONArray(jsonArrayAppointments);
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String yourRequiredField = jsonObject.getString("yourRequiredField");
}
} catch (JSONException e) {
e.printStackTrace();
}
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
login_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2196F3"
android:minHeight="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textSize="20sp"
android:gravity="center"
android:text="@string/text"/>
<!-- This will hold your Fragment -->
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</LinearLayout>