I am receiving the following two related errors in eclipse:
1) The method getIntent() is undefined for the type Fragment1
2) The method setDefaultPushCallback(Context, Class<? extends Activity>) in the type PushService is not applicable for the arguments (FragmentActivity, Class<Fragment1>)
Below is the portion of the code where the errors takes place:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container,
false);
ParseAnalytics.trackAppOpened(getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(getActivity(), Fragment1.class);
If you need any clarification, let me know.
1)You cannot call getIntent() inside a Fragment as it's not an Activity and so one does not inherit Activity's methods/ You should try using getActivity().getIntent() if you are really sure that this is what you are looking for.
2)This method asks you for a Context and you are passing it an Activity, and then asks you for a class that extends an activity and you are passing it a fragment. For the first argument i would call getActivity().getApplicationContext() and as a second argument getActivity().
Edit: Try casting the getActivity() like: (Activity) getActivity()
ParseAnalytics.trackAppOpened(((Activity)getActivity()).getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(((Activity)getActivity()).getApplicationContext(), ((Activity)getAcitivty()));
The two problems occur because you passed wrong parameters to them. Changes as following should help you get ride of them:
ParseAnalytics.trackAppOpened(getAcitivty().getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(getActivity(), getActivity());
But, whether this is what you want or not still depends on the needs of that two function.
Related
I Want to implement multi category in rss feed app, i created a method Downloader which works properly in mainactivity.
But Downloader Method not works in tab fragments.
Here is screenshot of my project and Downloader method.
click here for project screenshot
When i implement downloader method in fragment, it shows an error.
context c is not resolve here
here is code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
recyclerview = view.findViewById(R.id.rv);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
new Downloader(c,urlAddress,recyclerview).execute();
return view;
}
comment if you need any other information.
You defined in the signature of your class that the first parameter should be context. You try to pass an undefined variable called c in your code thats why c gets highlighted as error. To fix your code either define c or directly pass in an context into the constructor.
You can get the context by calling getContext()and it should work:
new Downloader(getContext(), urlAddress, recyclerview).execute();
i followed online tutorial on how to create android app.
i want to assign variable in onCreateView, but where is the method?
im using androidstudio latest btw.
please help
For an Activity the sort of equivalent method is onCreate(). You can override onCreateView() in a Fragment.
onCreateView is the method of a Fragment, rathen than an Activity.
As shown in the documentation:
onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
Confused, where is onCreateView method in MainActivity?
Nowhere. Activity does not have this method (it's Fragment's). Use onCreate() instead.
create methods in activity is
protected void onCreate(Bundle savedInstanceState) {}
create method in Fragment is
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
I am going through Head First Android Development and I am a bit confused with
this method --> findViewById(int id)
I have the below button in the file "activity_find_beer.xml" :
<Button
android:id="#+id/find_beer"
android:text="#string/find_beer"
android:onClick="onClickFindBeer" />
and the following code from the class FindBeerActivity.java which is taking the user selected beer and displaying the same in a textview.
public class FindBeerActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
//Call when the button gets clicked
public void onClickFindBeer(View view) {
//Get a reference to the TextView
TextView brands = (TextView) findViewById(R.id.brands);
//Get a reference to the Spinner
Spinner color = (Spinner) findViewById(R.id.color);
//Get the selected item in the Spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the selected item
brands.setText(beerType);
}
}
My Question is the method onClickFindBeer(View view) takes a View type of
object as a parameter , but in the xml i have just mentioned
android:onClick="onClickFindBeer" and when the user clicks the
button , the method onClickFindBeer gets invoked...Who is passing the object of
type View to the onClickFindBeer(View view) ...is it something
implicit ?
Second,on developer.android.com I see that the method
findViewById(int id) is both in the Activity class (
https://developer.android.com/reference/android/app/Activity.html
) and also in the View class
https://developer.android.com/reference/android/view/View.html
... It's not clear to me which class (Activity or View)
findViewById(int id) method is invoked when i call findViewById()
from onClickFindBeer(View view){}.
Would be highly obliged if someone could throw light on this.
Regards.
The method takes a View parameter as that is how it is implemented in a superclass of the Button class (It is public class Button extends TextView.). The views you add to XML are actually java classes. When you set a property to such an XML item, that constructs the object from the particular java class accordingly. The onClick method of the View class goes as onClick(View v). By setting an XML you just asked the Button class to look for the entered method but its signature is always with a View as a paramenter, which refers to the view clicked.
findViewById has to be called on a View group. But the Actyvity class implements it to search an item in view assigned to it by the setContentView() method.
It is done somewhat implicitly. When building your app, the XML file is actually converted into Java file. When you click the view, the view is passed into the onClickFindBeer(View view) function.
The findViewById() is being called here by the activity. You can see the method declaration by clicking on findViewByID while pressing Ctrl. For a view, you would have to call it using the view. For example,
view.findViewById();
Its called JAVA Reflection which is used by android
2.
As I know, main difference is that when you used OnClickListener from activity it is connected with partivular object such as Textview,Button
find_beer.setOnClickListener and below code is excuted when someButton is pressed.
While android:onClick = "onClickFindBeer" is used handle click directly in the view's activity without need to implement any interface
You have assigned the method onClickBeer to your button. When the button gets clicked, the object, in this case the button, is passed to the method you assigned to it. A Button is a type of View object, so you have a more generic View object as the parameter, but you are perfectly ok to caste it to a button object.
findViewById is called through a "context", which is a way of getting at system resources. You are asking the system to return to you a specific object, which you can then use. It is worth reading up on contexts.
Hope that answers some of your query.
Base on your sample above the android:onClick method is the one being invoked because when invoking a onclick method in java class, it need to call a onClickListener.
cause on the other question. as far as I know it belong to the view class because it always to reference an object on your design.
I want to change the text in text view in fragment1 by clicking a button in fragment2, i managed to do that by declaring the text view as static so i can change the text by Fragment1.textv.setText("hi"); , is that ok and what is the best way to do this.
static textv ;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
textv= (TextView)v.findViewById(R.id.textview);
return v;
}
Most common approach is using interfaces to 'speak' from fragment A to fragment B (you would need to communicate through the activity):
http://developer.android.com/training/basics/fragments/communicating.html
You can also use an Event-bus library like Otto, register the fragments that you want to update, and from the other fragment post an update:
http://square.github.io/otto/
as user3806331 stated you need to use interfaces ,follow the link and you will find what you need.but be aware that having static reference to a widget is a bad practice ,because it may lead to memory leaks in some situations.
I need to know, In which fragment callback method, we should call a web service by which after come back to fragment web service should not call again.
For example.
I have a fragment class MyFragment.java
public class MyFragment extends Fragment {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container,
false);
return rootView;
}
}
I need to know which callback method I have to call webservice to update the UI of fragment. Right Now I am calling web services from onCreateView method. but I need to know what should be best way to call web service from fragment.
If I understand your problem correctly, you want to fetch some data from the server and then inform the fragment that data is prepared and redraw the fragment, is that correct? According to the documentation here:
onCreate() -
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
When you create a Fragment somewhere else in your application, onCreate() method is called. When fragment has to be drawn for the first time, onCreateView() is called and this method returns a created View. In your case, you could probably go with something like:
Declare an instance variable (container) for this data and adapter (if you use any).
In onCreate, initialize all this data (empty container), initialize adapter and then execute the AsyncTask.
In onCreateView, prepare the view to return - adapter etc. So now, once AsyncTask will finish, in onPostExecute it calls your_adapter.notifyDataSetChanged(). This will redraw the fragment, since adapter will be informed that data has changed (fetched from the server).
Depends on when you want to fetch the data.
Do you want it every time the app comes to the foreground?
Use onResume()
Do you want it only when the app starts for the first time?
Use onViewCreated(), which gets called after onCreateView is finished.
onCreate() is tricky.
Put everything that you want to call just once in onCreate() method.
For example your API call, list, adapter initialization.
So ideally I would do like this
public void onCreate(Bundle sis) {
getImagesFromServer(); // API Call
initialzeLists()
initializeAdapters()
}
public View onCreateView(LayoutInflater i, ViewGroup c,Bundle sis) {
recyclerView = findViewById()
setUpAdapters()
return rootView;
}
TRICKY PART
But if you really want onCreate() to be called once then reuse Fragment. (Don't create a new instance every time).
How to reuse a Fragment?
While Adding for first time use add it to backStack with a KEY like this
currentFragment = FirstFragment.getInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentHolder, currentFragment, "FIRST_KEY")
.addToBackStack("FIRST_KEY")
.commit();
And when you want to add it again get the old instance with the KEY like this
currentFragment = getSupportFragmentManager().findFragmentByTag("FIRST_KEY");
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentHolder, currentFragment, "FIRST_KEY")
.addToBackStack("FIRST_KEY")
.commit();
Demo project:
Here is the link to a demo project which loads a list of images using the Volley library. It has a similar implementation.
Woof - Use fragments the right way