Presenter in MVP(Android) gets deleted/gets null inside Fragment - android

I am using MVP. My activity contains one Fragment. I am initializing and then setting presenter to fragment inside Main-Activity's on Create method as follow.
public class MainActivity extends AppCompatActivity {
private StashPresenter stashPresenter;
private MainFragment mainFragment;
FragmentManager fm;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.getInstance().getAppComponent().inject(this);
setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.fragment_container, MainFragment.newInstance(),"mainFragment")
.commitNow();
mainFragment = (MainFragment) fm.findFragmentById(R.id.fragment_container);
stashPresenter = new StashPresenter(mainFragment);
mainFragment.setPresenter(stashPresenter);
}
Inside my mainFrgament class I am settinf Presenter in setPresenterFunction as follow.
public class MainFragment extends Fragment implements
StashContract.PublishToView {
public StashContract.ToPresenter forwardInteraction;
public void setPresenter(StashContract.ToPresenter forwardInteraction)
{
this.forwardInteraction = forwardInteraction;
}
Sometimes while performing searching operation as shown in my following code inside OnCreateView of mainFragment, I gets an error saying my forward
"Attempt to invoke interface method on a null object reference"
Sometime I get this error, sometimes I do not. I do not understand why this is happening
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
unbinder = ButterKnife.bind(this, view);
searchView.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
progressBar.setVisibility(View.VISIBLE);
forwardInteraction.searchButtonClick(actionId, searchView.getText().toString());
return true;
}
return false;
});
String[] artistNames = getResources().getStringArray(R.array.artistNamesSuggestion);
ArrayAdapter<String> adapterArtist = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.fragment_main, R.id.search_phrase, artistNames);
searchView.setAdapter(adapterArtist);
searchView.setThreshold(1);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new SpaceItemDecoration(space, space, space, space));
return view;
}

Solution
In the "onCreateView" method of the fragment, only initialize the views.
Move rest of the code in the "onResume" method.
Reason for error
Check this image
As you can see, "onCreateView" of your fragment is called when you are in the "onCreate" method in you activity.
In the current state of your code, there maybe times you when you will try to use the presenter before it is initialized.
Therefore, set your presented in the "onCreate" method of your activity and use it either it on the "onStart" or "onResume" of your fragment.
You can check this project to understand the MVP architecture more.

Related

Can't find where context is missing

I've been refactoring a previous project to use fragments instead of creating separate activities as a school assignment. I've been trying to find out where this error is for close to an hour now and no luck. I get context missing when I add this line canvas.setColor(color, position);
Here's the Main activity:
public class MainActivity extends AppCompatActivity implements PaletteFragment.SpinnerSelectedInterface {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PaletteFragment palette = PaletteFragment.newInstance();
Bundle bundle = new Bundle();
bundle.putStringArray(KeyData.PASS_COLOR, getResources().getStringArray(R.array.colors));
bundle.putStringArray(KeyData.PASS_POSITION, getResources().getStringArray(R.array.colorNames));
palette.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.palette_fragment, palette).commit();
}
#Override
public void setCanvasColor(String color, int position) {
CanvasFragment canvas = CanvasFragment.newInstance(null);
getSupportFragmentManager().beginTransaction().add(R.id.canvas_fragment, canvas).addToBackStack(null).commit();
canvas.setColor(color, position);
}
}
And here's the fragment:
public class CanvasFragment extends Fragment {
private TextView displayColor;
private View background;
public CanvasFragment() {
// Required empty public constructor
}
public static CanvasFragment newInstance(Bundle bundle) {
CanvasFragment fragment = new CanvasFragment();
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_canvas, container, false);
displayColor = (TextView) v.findViewById(R.id.displayColor);
background = (View) v.findViewById(R.id.canvas_fragment);
return v;
}
public void setColor(String color, int position){
String[] names = getResources().getStringArray(R.array.colorNames);
displayColor.setText(names[position]);
background.setBackgroundColor(Color.parseColor(color));
}
I've tried override onAttach and onDetach on the canvas fragment and adding a message listener, but still get the error as well. Would appreciate anything that could steer me in the right direction.
Error
java.lang.IllegalStateException: Fragment CanvasFragment{69925d5 (240edefb-318c-4983-bd15-cf45142e849a) id=0x7f080047} not attached to a context.
When you call
getSupportFragmentManager()
.beginTransaction()
.add(R.id.canvas_fragment, canvas)
.addToBackStack(null)
.commit();
You're using commit(). As per its Javadoc:
Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
So when you immediately call canvas.setColor(color, position) directly afterwards, the Fragment is not attached and it doesn't yet have a Context associated with it.
If you want the Fragment to be immediately added, you want to use commitNow(), which forces the transaction to happen immediately. This ensures that it'll be done before your setColor method is called:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.canvas_fragment, canvas)
.addToBackStack(null)
.commitNow();

How can I replace a Fragment from within onTouch() of another Fragment?

I need to display a Fragment when the user touches the TextView of another Fragment. Here's what I have:
public class FindPeopleHelpFragment extends Fragment {
public FindPeopleHelpFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find_people_help, container, false);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.find_people_help);
TextView welcome_home = (TextView) view.findViewById(R.id.find_people_help);
welcome_home.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
EditProfileFragment editProfileFragment = new EditProfileFragment();
FragmentManager manager = getActivity().getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragment_container, editProfileFragment, editProfileFragment.getTag()).commit();
}
return true;
}
});
// Inflate the layout for this fragment
return view;
}
}
The issue is that onTouch() returns a boolean, but when replacing the Fragment, I need to return a View. Otherwise I get the following.
10-05 15:00:51.057 25152-25152/wegrok.chiaramail.com E/AndroidRuntime: FATAL EXCEPTION: main
Process: wegrok.chiaramail.com, PID: 25152
java.lang.IllegalArgumentException: No view found for id 0x7f0f00b1 (wegrok.chiaramail.com:id/fragment_container) for fragment FindPeopleHelpFragment{c052157 #1 id=0x7f0f00b1}
Anyone know how to deal with this?
You need to make these Fragment transactions from your Activity. The Activity that hosted the Fragment should have a public function for handling the fragment switching so that when the TextView of your Fragment1 is clicked, the public method of the Activity will be called to make the Fragment switch.
So you might consider having an Activity like this.
public class YourActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
// The Fragment 1 will be loaded by default
switchToFragment1();
}
public void switchToFragment1() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new Fragment1()).commit();
}
public void switchToFragment2() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new Fragment2()).commit();
}
}
Now from your Fragment1, you need to implement an onClick listener in to the TextView, so that when the TextView is clicked, Fragment1 switches to Fragment2.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((YourActivity) getActivity()).switchToFragment2();
}
});)
Hope that helps.

Getting value from Fragment into mainActivity

I have a MainActivity. It's layout contains a fragmentContainer and an actionBar. Then I have 2 different fragments containing various EditTexts.All of them have Ids. I populate the fragmentContainer with the fragments when user clicks a button in actionBar. Everything works perfectly.
Now...the app, is supposed to collect the contents of all EditTexts from both fragments when I push a button (in the menu of the actionBar). But it does not work. It crashes when I try to access the EditTexts directly from the activity. I understand that I should create a public method inside the code of the fragments that will return the values I am interested in. But I seem to be doing something wrong because the method is not accessible from the main (TabActivity)
Here is my code:
public class TabActivity extends Activity {
ActionBar.Tab tab_alocare, tab_merc;
Fragment fragmentTab1 = new aloc_fragment();
Fragment fragmentTab2 = new merc_fragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab); // this layout contains the fragmentContainer
...// here I do some actionBar stuff
}
...
}
An example of one of the fragments is bellow:
public class aloc_fragment extends Fragment {
EditText mEditText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_aloc, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mEditText = (EditText) getView().findViewById(R.id.edaloc);
}
public String getMyText() {
String rez = "";
if (mEditText.getText()!=null) {
rez = mEditText.getText().toString();
}
return rez;
}
}
Now... if inside the Activity code I want to access the edaloc EditText using findViewById, it crashes the app with nullException. So I then tried to access the public method of the fragment so that it would return me the value that I need. But "getMyText" method is not accessible from the TabActivity.
So, this code does not work:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
Fragment alocfragment = (Fragment) getFragmentManager().findFragmentByTag("aloc");
String s = alocfragment.getMyText(); // this is not compiling at all
I am clearly doing something wrong.
Please advise.
Thank you
There are lots of mistakes in your code, You have neither declared nor initialize the fragments at proper place. Also, you are trying to create another instance of fragment in onOptionsItemSelected which is wrong, you need to use same instance of fragment there to access your functions with values. I hope you know how to add and inflate fragment. See my comments and code changes.
Try with this -
public class TabActivity extends Activity {
ActionBar.Tab tab_alocare, tab_merc;
//Declare fragments here
Fragment fragmentTab1;
Fragment fragmentTab2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab); // this layout contains the fragmentContainer
//Initialize here
fragmentTab1 = new aloc_fragment();
fragmentTab2 = new merc_fragment();
//inflate/add fragments here to the activity
...// here do your actionBar stuff
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
//Now, use same instance of fragment to access your function
String s = fragmentTab1.getMyText();
}
}
public class aloc_fragment extends Fragment {
EditText mEditText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_aloc,
container, false);
//hold edittext here in the variable
mEditText = (EditText) view.findViewById(R.id.edaloc);
return view;
}
public String getMyText() {
String rez = "";
if (mEditText.getText()!=null) {
rez = mEditText.getText().toString();
}
return rez;
}
}
Also, must to go through Fragment tutorial on Android Developer Fragment Link Link 2, Using Fragments

Android buttons' onClickLIstener() only works once when the Activity begins, not when returning from Fragment transaction

Any time the main Activity is started or restarted (i.e. by pressing the home button on my action bar), all of the buttons (which reside in a fragment in this main activity) work correctly. However, if I press one of the buttons, then press the back button, the home fragment and its buttons reappears but none of the button callbacks work. The button images continue to change appearance when pressed, as always.
The callbacks are assigned in the onCreateView method of the home fragment, as follows:
public class HomeFragment extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button = (Button) v.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d(TAG, "Button!");
mActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.contFragMain, new SomeFragment())
.addToBackStack(null)
.commit();
}
});
}
Here is the superclass of Fragments I launch with the buttons:
public abstract class RouteListFragment extends SherlockFragment {
private static final String TAG = RouteListFragment.class.getName();
private Activity mActivity; // parent activity
RouteListAdapter mAdapter;
ListView mListViewRouteList;
public RouteListFragment() {
super();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bndl = getArguments(); // data passed should contain search term
View v = inflater.inflate(R.layout.fragment_route_list, container, false);
mListViewRouteList = (ListView) v.findViewById(R.id.listViewRouteList);
addRoutesToList(); //Populates the list with data via an async DB call
return v;
}
You shouldn't be stashing the Activity reference; the Activity is probably getting recreated at some point (depends on how your flow is), but you should either reassign the reference in onAttach (Activity activity), or just call getActivity() to get the currently Activity you're attached to.
So mActivity.getSupportFragmentManager()...
should instead be
getActivity().getSupportFragmentManager()...
EDIT: Wait, maybe that's not right. What is v in onCreateView()? You're not creating any UI in this Fragment? You should be using onViewCreated() to get a reference to the views in the created hierarchy.

Make AsyncTask inside fragment run after FragmentTransaction.commit()

I have a FragmentTransaction in the activity, and I want the AsyncTask to run in the fragment after fragmentTransaction.commit(). How do I make AsyncTask run after commit? Because AsyncTask runs 2 times before and after the commit. Can anyone help?
*note: AsyncTask run with String of the activities that I sent using FragmentTransaction
Activity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menudisplay);
Bundle extras = getIntent().getExtras();
language = extras.getString("language");
lang_id = extras.getInt("id");
MyListFragment mylist = new MyListFragment();
FragmentTransaction fragmentTrans = getFragmentManager()
.beginTransaction();
mylist.language = language;
fragmentTrans.add(mylist, "language");
fragmentTrans.commit();
Log.d("tes", "data loaded");
}
Fragment :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_menulist_overview, container,
false);
System.out.println("tes="+language+" sama ini = "+asdf);
onActivityCreated(inflater, container, savedInstanceState);
return view;
}
public void onActivityCreated(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
run();
}
public void run(){
Log.d("Test","How many called?");
if(language != null)
new loadcat().execute();
}
I didn't get the note you wrote but you can run the AsyncTask after you have .commit() the transaction simple by creating a public method inside of the fragment that executed you desired AsyncTask and to call it from the Activity at the desired point in code.
So for example if you crate this method inside the Fragment:
public void executeAsyncTask(String language, int lang_id)
{
...
asyncTask.execute(language, lang_id);
}
And in Activity, you can call this method:
...
fragmentTrans.commit();
Log.d("tes", "data loaded");
mylist.executeAsyncTask(language,lang_id);
As far as I know, commit execution callback is not exposed(or maybe even doesn't exist).
So the question is - when do you want to execute your AsyncTask?
If you want it to be executed once at the fragment creation, your code is right, except you should remove your explicit onActivityCreated() call from onCreateView like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_menulist_overview, container,
false);
System.out.println("tes="+language+" sama ini = "+asdf);
onActivityCreated(inflater, container, savedInstanceState); // REMOVE THIS LINE
return view;
}
onActivityCreated is a callback. Calling it explicitly cause double execution of your task

Categories

Resources