I use an ArrayAdapter to show items in a ListView. Every row in this ListView owns a button.
Whenever the user clicks on one of these buttons I start an AsyncTask to do some processing in the background.
This is working so far.
Now I want to show a custom ProgressDialog during this time. What puzzles me here is the first parameter of the static convinience method ProgressDialog.show(). Within an activity I usually use "Activityname.this" here. But what should I use in an adapter. I tried context from the adapter (that crashed), context.getApplicationContext and several more. Nothing worked - either crashed or is refused from the compiler.
So my question today: What should I put into this parameter?
Here's a stripped down part of my code:
public class MyAdapter extends ArrayAdapter<MyContainer> {
private class MyAsyncTask extends AsyncTask<String, Void, Boolean> {
#Override
protected void onPreExecute () {
if (!isRunning) {
progressDialog = MyProgressDialog.show(?????,
null,
null,
true,
false);
}
}
#Override
protected Boolean doInBackground(final String... strings) {
boolean rc = false;
if (!isRunning) {
isRunning = true;
//
rc = true;
}
return rc;
}
#Override
protected void onPostExecute(final Boolean result) {
if (progressDialog != null) {
progressDialog.cancel();
}
progressDialog = null;
//
}
}
private class MyOnClickListener implements OnClickListener {
private MyContainer container;
public MyOnClickListener(final MyContainer container) {
this.container = container;
}
public void onClick(final View view) {
if (container != null) {
new MyAsyncTask().execute(container.getUrl());
}
}
private String appName = "";
private ArrayList<MyContainer> containers;
private Context context;
private boolean isRunning;
private int layout;
private MyProgressDialog progressDialog;
private Resources resources;
public MyAdapter(final Context context, final int layout, final ArrayList<MyContainer> containers, final long link_id) {
super(context, layout, containers);
this.context = context;
this.layout = layout;
this.containers = containers;
resources = context.getResources();
appName = resources.getString(R.string.txt_appname);
}
#Override
public View getView(final int position, final View contentView, final ViewGroup viewGroup) {
//
}
}
Thanks in advance.
EDIT: Using the instance variable context from the adapter worked after cleaning the project. Arg! Thanks for your answers.
Hi :D well if you see your constructor of adapter
public MyAdapter(final Context context, final int layout, final ArrayList<MyContainer> containers, final long link_id) {
super(context, layout, containers);
this.context = context;
you pass context so in side of adapter you use context :D to build progress dialog
Related
My app crashes every time the Splash Screen has slept for 5 seconds and it just won't start my slider.I'd like to add by saying that I've tried using Shared Preferences but the error tends to persist.Any help would be appreciated.The method launchmain2() is basically nothing but calling a blank activity named Main2Activity.I haven't created as many layouts for the slider as I would need but rather just one which gets all its resources accordingly from the Slider class.Here's the full code
MainActivity
public class MainActivity extends AppCompatActivity {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.welcome_image);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.transition);
iv.setAnimation(animation);
Thread loading = new Thread() {
public void run() {
try {
sleep(5000);
Intent main = new Intent(getApplicationContext(),Slide_Adapter.class);
startActivity(main);
finish();
}
catch (Exception e) {
e.printStackTrace();
}
}
};
loading.start();
}
}
Slide_Adapter
public class Slide_Adapter extends AppCompatActivity {
ViewPager pager;
Slider adapter;
Preferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide__adapter);
pager = findViewById(R.id.viewpager);
adapter = new Slider(this);
pager.setAdapter(adapter);
preferences = new Preferences(this);
if(!preferences.First()){
launchmain2();
finish();
}
}
private void launchmain2() {
preferences.FirstTime(false);
Intent intent = new Intent(Slide_Adapter.this, Main2Activity.class);
startActivity(intent);
finish();
}
}
Slider
public class Slider extends PagerAdapter {
private Context context;
public Slider(Slide_Adapter slide_adapter) {
this.context = context;
}
public int images[] = {R.drawable.add, R.drawable.call, R.drawable.message};
public String title[] = {"ADD A CONTACT", "MAKE CALLS", "TEXT"};
public int background[] = {
Color.rgb(255,0,0),
Color.rgb(128,255,0),
Color.rgb(255,0,255)};
#Override
public int getCount() {
return title.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == (RelativeLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.slides, container, false);
RelativeLayout relativeLayout = view.findViewById(R.id.relative_layout);
ImageView imageView = view.findViewById(R.id.image);
TextView textView = view.findViewById(R.id.description);
relativeLayout.setBackgroundColor(background[position]);
imageView.setImageResource(images[position]);
textView.setText(title[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
}
Preference Class
public class Preferences {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Context context;
private static final String FIRST_LAUNCH = "A";
int MODE = 0;
private static final String PREFERENCE = "B";
public Preferences(Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences(PREFERENCE, MODE);
editor = sharedPreferences.edit();
}
public void FirstTime(boolean first){
editor.putBoolean(FIRST_LAUNCH, first);
editor.commit();
}
public boolean First(){
return sharedPreferences.getBoolean(FIRST_LAUNCH, true);
}
}
This issue arises because of the null context. Update context on Slider Adapter page.
Context update
private Context mContext;
public Slider(Context context) {
this.mContext = context;
}
And then use the mContext for instantiating the item.
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Update: For opening another activity for second time opening, change your MainActivity like this.
public class MainActivity extends AppCompatActivity {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Preferences.init(getApplicationContext());// Also add this
iv = findViewById(R.id.welcome_image);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.transition);
iv.setAnimation(animation);
Thread loading = new Thread() {
public void run() {
try {
sleep(5000);
if(Preferences.getIsFirst() == false){
Preferences.writeFirstTimeOpen(true);
Intent main = new Intent(getApplicationContext(),Slide_Adapter.class);
startActivity(main);
finish();
}else{
Intent main = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(main);
finish();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};
loading.start();
}
}
And Preference Class:
public class Preferences {
private static SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
Context context;
private static final String FIRST_LAUNCH = "A";
int MODE = 0;
private static final String PREFERENCE = "B";
public static void init(Context context) {
if (sharedPreferences == null) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
}
public static boolean writeFirstTimeOpen(boolean value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_LAUNCH, value);
return editor.commit();
}
public boolean getIsFirst(){
return sharedPreferences.getBoolean(FIRST_LAUNCH, true);
}
}
Please check this, as this will also fix another first time opening issue.
You need to modify like this your constructor
public Slider(Context context) {//Add Context in parameter
this.context = context;
}
And please go though this link how to set and get value from shared-preferences
Hope this helps you
I noticed that parameter in constructor of Slider class not define as needed
What you do
public Slider(Slide_Adapter slide_adapter) {//There is not Context in parameter
this.context = context;//context will be still null
}
What need to do
public Slider(Context context) {//Add Context in parameter
this.context = context;
}
Here I'm trying to make a quiz application without using databases (requirement). Each question has 4 options.
I had made a class for Questions. Now, in the activity in which I want to show my data, I'm unable to get method to fetch the data from the QuestionModelClass.
I had made 2D Array but it gets more complicated to get it. Is there any way to bind 3 of the classes (QuestionModelClass, Adapter class, and Activity class)?
public class QuestionsModelClass {
private String sQuestion;
private String sRightAnswer;
private List<String> sOptions;
QuestionsModelClass(){
sQuestion = null;
sRightAnswer = null;
sOptions = null;
}
public QuestionsModelClass(String sQuestion, String sRightAnswer, List<String> sOptions) {
this.sQuestion = sQuestion;
this.sRightAnswer = sRightAnswer;
this.sOptions = sOptions;
}
public String getsQuestion() {
return sQuestion;
}
public void setsQuestion(String sQuestion) {
this.sQuestion = sQuestion;
}
public String getsRightAnswer() {
return sRightAnswer;
}
public void setsRightAnswer(String sRightAnswer) {
this.sRightAnswer = sRightAnswer;
}
public List<String> getsOptions() {
return sOptions;
}
public void setsOptions(List<String> sOptions) {
this.sOptions = sOptions;
}
}
And my Adapter Class
public class QuizAdapter extends BaseAdapter {
private Context context;
private List<QuestionsModelClass> questionClassList;
private String[][] options;
private LayoutInflater inflater;
private QuizAdapter(Context c, List<QuestionsModelClass> l){
this.context= c;
this.questionClassList = l;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return questionClassList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.questionpattern, parent,false);
QuestionsModelClass questions = questionClassList.get(position);
TextView quesText= convertView.findViewById(R.id.questionTextView);
RadioButton radioButtonA = convertView.findViewById(R.id.optionA);
RadioButton radioButtonB = convertView.findViewById(R.id.optionB);
RadioButton radioButtonC = convertView.findViewById(R.id.optionC);
RadioButton radioButtonD = convertView.findViewById(R.id.optionD);
return convertView;
}
And this is the Activity class in which I am trying to implement all the functions
public class QuizActivity extends Activity {
final Context context= this;
private List<QuestionsModelClass> classObject;
Button okayButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
String[] question= new String[]{"Q1. ABDE", "Q2. ADDASD"};
String[][] op;
String[] right = new String[]{"abc","def"};
classObject = new ArrayList<>();
op= new String[][]{
{"a1", "2", "3", "4"},
{"b1","b2","b3","b4"}};
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.customdialoguebox);
dialog.show();
okayButton = (Button) dialog.findViewById(R.id.okayButton);
okayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,"Good Luck!", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
}
I am currently in trouble with my project.
I am trying to separate functions into different classes instead of putting all in Activity class.
So I think I need to pass "view" from SettingsActivity to ConfigPresenter, from ConfigPresenter to ConfigEventHandler.
but I don not know why it doesn't work at all.
can anyone help me to know what the problem is?
Appreciate all the time and sorry that I only come stackoverflow to get information.
I hope I become a professional someday and help people here. :)
I make errors bold below.
SettingsActivity.java
- configPresenter.optionClicked **(view)**
ConfigPresenter.java
- eh.checkOption **(view)**
ConfigEventHandler.java
- AlertDialog.Builder builder = new AlertDialog.Builder **(SettingsActivity.class)**
- LayoutInflater inflater = SettingsActivity.class.**getLayoutInflater()**
- TextView titleUrl = (TextView) **findViewById**(title);
- TextView optionUrl = (TextView) **findViewById**(option);
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity implements ConfigPresenter.View {
private ConfigPresenter configPresenter;
private Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public void optionClicked(View view) {
configPresenter.optionClicked(view);
}
}
ConfigPresenter.java
public class ConfigPresenter {
private View view;
private ConfigEventHandler eh;
private ConfigFileHandler fh;
public ConfigPresenter(ConfigPresenter.View view) {
this.view = view;
eh = new ConfigEventHandler();
fh = new ConfigFileHandler();
}
public void optionClicked(View view) {
eh.checkOption(view);
Log.d("Config", "Presenter");
}
}
ConfigEventHandler.java
public class ConfigEventHandler {
public void checkOption(View view) {
if ( view.getId() == R.id.layout_url ) {
showDialog(R.id.title_url, R.id.option_url);
} else if ( view.getId() == R.id.layout_port ) {
showDialog(R.id.title_port, R.id.option_port);
} else {
showDialog(R.id.title_path, R.id.option_path);
}
}
public void showDialog(int title, int option) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.class);
LayoutInflater inflater = SettingsActivity.class.getLayoutInflater();
View content = inflater.inflate(R.layout.dialog, null);
builder.setView(content);
TextView titleUrl = (TextView) findViewById(title);
TextView dialogTitle = (TextView) content.findViewById(R.id.dialog_title);
TextView optionUrl = (TextView) findViewById(option);
EditText dialogOption = (EditText) content.findViewById(R.id.dialog_option);
dialogTitle.setText(titleUrl.getText());
dialogOption.setText(optionUrl.getText());
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// ok
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel
}
})
.show();
}
}
First of all, your classes aren't logical, but a few fixes for your current setup.
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setcontentview etc.
}
public void optionClicked(View view){
ConfigPresenter.optionClicked(view,getLayoutInflater(),this);
}
}
ConfigPresenter
public class ConfigPresenter {
public static void optionClicked(View view, LayoutInflater inflater, Context context){
ConfigEventHandler.checkOption(view,inflater,context);
}
}
ConfigEventHandler
public class ConfigEventHandler {
public static void checkOption(View view, LayoutInflater inflater, Context context){
showDialog(inflater,context);
}
private static void showDialog(LayoutInflater inflater, Context context){
AlertDialog dialog = new AlertDialog.Builder(context).create();
View content = inflater.inflate(R.layout.alert_add_item,null);
dialog.setContentView(content);
dialog.show();
}
}
In SettingsActivity:
private ConfigPresenter configPresenter;
private Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Initialize configPresenter:
configPresenter = new ConfigPresenter(this)
}
And in the constructor method in ConfigPresenter, you can remove the weird ConfigPresenter.View like this:
public class ConfigPresenter {
private ConfigEventHandler eh;
private ConfigFileHandler fh;
private Context context;
public ConfigPresenter(Context context) {
this.context = context;
eh = new ConfigEventHandler(context);
fh = new ConfigFileHandler();
}
/* ... */
}
EDIT:The Context also needs to be passed down. Edited code above, and the edits below also needs to be added.
public class ConfigEventHandler {
private Context context;
// Constructor with Context
public ConfigEventHandler(Context context) {
this.context = context;
}
/* ... */
public void showDialog(int title, int option) {
// Passing the context to the Builder:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
/* ... */
}
However - this approach seems to only cause a lot of headache. Why do you want to split up your code in so many classes?
I'm trying to adapt Mortar&Flow in my app and faced with an issue, that I can't make PageAdapter works with Screens, instead of Fragments.
Anyone managed to get it right?
I didn't succeed but, probably someone can guide me from this point:
The initial Dagger Registration:
#Module(
injects = {
MainActivity.class,
},
library = true,
complete = false
)
public class DaggerConfig {
#SuppressWarnings("unused")
#Provides #Singleton Gson provideGson() {
return new GsonBuilder().create();
}
}
MainScreen, whose View is hosting ViewPager:
#Layout(R.layout.screen_main) #WithModule(MainScreen.Module.class)
public class MainScreen extends Path {
#dagger.Module(injects = MainView.class, addsTo = DaggerConfig.class)
public static class Module {}
#Singleton
public static class Presenter extends ViewPresenter<MainView> {
#Inject
public Presenter() {}
}
}
MainView:
...........
#Inject
MainScreen.Presenter presenter;
...........
#Override protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.inject(this);
final Path[] screens = {
new SubScreen("1"),
new SubScreen("2"),
new SubScreen("3"),
};
CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(getContext(), screens );
customPagerAdapter .setAdapter(firstRunPagerAdapter);
}
.....
Now, the main part, SubScreen (3 similar screens, that differs only by the parameters we are passing into them => they should adjust views according these parameters)
#Layout(R.layout.screen_subscreen) #WithModule(SubScreen.Module.class)
public class SubScreen extends Path {
private final String title;
public SubScreen(String titleParam) {
title = titleParam;
}
#dagger.Module(injects = SubView.class, addsTo = DaggerConfig.class)
public class Module {
#Provides
SubViewMetadata provideSubViewMetadata() {
return new SubViewMetadata(backgroundColor, title);
}
}
#Singleton
public static class Presenter extends ViewPresenter<SubView> {
private String title;
#Inject
public Presenter(String title) {
this.title= title;
}
#Override
protected void onLoad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
if (!hasView()) {
return;
}
getView().setTitle(subViewMetadata.title);
}
}
}
and it's view
public class SubView extends FrameLayout {
#InjectView(R.id.subViewTitleTextView)
TextView subViewTitleTextView;
#Inject
SubScreen.Presenter presenter;
public SubView(Context context, AttributeSet attrs) {
super(context, attrs);
ObjectGraphService.inject(context, this);
}
public void setTitle(String title) {
subViewTitleTextView.setText(title);
}
#Override protected void onAttachedToWindow() {....}
#Override protected void onDetachedFromWindow() {....}
......
}
Custom Pager adapter:
public class CustomPagerAdapter extends PagerAdapter {
private final Context context;
private final Path[] screens;
public CustomPagerAdapter(Context context, Path[] screens) {
this.context = context;
this.screens = screens;
}
#Override
public int getCount() {
return (screens == null)? 0 : screens.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view.equals(o);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Path screen = screens[position];
MortarScope originalScope = MortarScope.getScope(context);
MortarScope newChildScope = originalScope.buildChild().build("tutorialpage" + position);
Context childContext = newChildScope.createContext(context);
View newChild = Layouts.createView(childContext, screen);
container.addView(newChild);
return newChild;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = ((View) object);
container.removeView(view);
MortarScope.getScope(view.getContext()).destroy();
}
}
The problem statement: it's crashing, as SubView class hasn't been added into list of Injections at the "Layouts.createView(childContext, screen);" moment in the Adapter, and I can't add it by default, because I want to have a #provider of data from SubScreen to SubScreen.Presenter. (I'm using local variable.
If I add SubView.class into list of injections and convert local Screen's variables into static, then I'll have 3 identical pages inside the ViewPager (which is logical, as every next call of the constructor - overrides old static variables).
Any help/ideas?
Thanks for your help,
Konstantin
Ok, I figured out.
First of all, adding SubView into list of globally injected classes
Then modifying SubScreen class:
#Layout(R.layout.screen_subscreen)
public class SubScreen extends Path {
private static String titleStatic; // Introducing static variable
private final String title;
public SubScreen(String titleParam) {
title = titleParam;
}
public void refreshPresenter() {
titleStatic = title;
}
#Singleton
public static class Presenter extends ViewPresenter<SubView> {
private String title;
#Inject
public Presenter() {
}
#Override
protected void onLoad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
if (!hasView()) {
return;
}
getView().setTitle(titleStatic);
}
}
}
and then in Custom adapter do this changes:
public class CustomPagerAdapter extends PagerAdapter {
private final Context context;
private final SubScreen[] screens;
public CustomPagerAdapter(Context context, SubScreen[] screens) {
this.context = context;
this.screens = screens;
}
......
#Override
public Object instantiateItem(ViewGroup container, int position) {
SubScreen screen = screens[position];
MortarScope originalScope = MortarScope.getScope(context);
MortarScope newChildScope = originalScope.buildChild().build("tutorialpage" + position);
Context childContext = newChildScope.createContext(context);
screen.refreshPresenter(); // updating the static var with local one!
View newChild = Layouts.createView(childContext, screen);
container.addView(newChild);
return newChild;
}
....
}
I.e. the solution is to keep the local AND static variables in the Screen, if the same screen is going to be reused. And when we inflate the view it - just setting the right value to the static one (that would be used in the Presenter).
I am not sure, that it is the best possible solution, but it works. It would be nice to hear, if it can be improved.
I need to have a Relative Layout that's Serializable because I need to pass it in the Bundle of the creation of a newInstance of a Fragment (just casting it in to Serializable didn't work).
I did like this.
import java.io.Serializable;
public class SerializableRelativeLayout extends RelativeLayout implements Serializable {
public SerializableRelativeLayout(Context context){
super(context);
}
public SerializableRelativeLayout(Context context, AttributeSet attributeSet){
super(context, attributeSet);
}
public SerializableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
In the layout files I use it like this:
<packagename.utils.SerializableRelativeLayout
It's working fine, but the problem is that if I go to the background, or start a new activity, when going back to the previous activity that contains a SerializableRelativeLayout, the app crashes with the messages:
java.io.InvalidClassException: android.widget.RelativeLayout; IllegalAccessException
and
java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = packagename.utils.SerializableRelativeLayout
Fragments without this, I have no problem. So I'm guessing I need to save the instance of the fragments (I know I should already be doing this). Then comes the problem, I'm using a fragmentStatePageAdapter, and I couldn't find anywhere how to save it. These are my classes.
public class GalleryPagerAdapter extends FragmentStatePagerAdapter {
private List<Page> pageList;
private SerializableRelativeLayout parentActivity;
private String pathToTrack;
public GalleryPagerAdapter(FragmentManager fm, List<Page> pageList, SerializableRelativeLayout parentActivity, String pathToTrack) {
super(fm);
this.pageList = pageList;
this.parentActivity = parentActivity;
this.pathToTrack = pathToTrack;
}
#Override
public Fragment getItem(int i) {
return ContentGalleryFragment.newInstance(pageList.get(i), pageList.size(), parentActivity, pathToTrack);
}
#Override
public int getCount() {
return pageList.size();
}
}
And
public class ContentGalleryFragment extends Fragment {
private final static String PAGE = "page";
private final static String PAGE_COUNT = "pageCount";
private final static String RELATIVE_LAYOUT = "relativeLayout";
private final static String PATH = "pathToTrack";
private Page contentPage;
private int pagesCount;
private SerializableRelativeLayout relativeLayout;
private String pathToTrack;
public ContentGalleryFragment(){
}
public final static ContentGalleryFragment newInstance(Page contentPage, int pagesCount, SerializableRelativeLayout relativeLayout, String pathToTrack) {
ContentGalleryFragment fragment = new ContentGalleryFragment();
Bundle bundle = new Bundle(4);
bundle.putSerializable(PAGE, contentPage);
bundle.putInt(PAGE_COUNT, pagesCount);
bundle.putSerializable(RELATIVE_LAYOUT, relativeLayout);
bundle.putString(PATH, pathToTrack);
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_content_gallery, container, false);
contentPage = (Page) getArguments().getSerializable(PAGE);
pagesCount = getArguments().getInt(PAGE_COUNT);
relativeLayout = (SerializableRelativeLayout) getArguments().getSerializable(RELATIVE_LAYOUT);
pathToTrack = getArguments().getString(PATH);
ContentDetailHelper contentHelper = new ContentDetailHelper(relativeLayout, pathToTrack);
contentHelper.createContentDetailsPage(contentPage, pagesCount, inflater, rootView.findViewById(R.id.fragment_content_gallery_linearLayout_root));
return rootView;
}
#Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(PAGE, contentPage);
outState.putInt(PAGE_COUNT, pagesCount);
outState.putSerializable(RELATIVE_LAYOUT , relativeLayout);
outState.putString(PATH, pathToTrack);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
//probably orientation change
contentPage = (Page) savedInstanceState.getSerializable(PAGE);
pagesCount = savedInstanceState.getInt(PAGE_COUNT);
relativeLayout = (SerializableRelativeLayout) savedInstanceState.getSerializable(RELATIVE_LAYOUT);
pathToTrack = savedInstanceState.getString(PATH);
}
}
}
I know we should Override onSaveInstanceState when we are doing a simple Activty / Fragment . But I couldn't find how to do with a ViewPager.
It's my first question here, so, sorry if it's too long or not well explained/wrong title.
Thanks in advance.