I'm going to create a Fragment that have an Activity to managing that fragment; Like this:
public class Form extends Fragment {
// TODO some code ...
public class Dialog extends FragmentActivity {
// TODO some code ..
}
}
but it show me error, So I crated it with a static inner Activity but an static inner class don't get me that accesses.
There's no way to make this work. Android requires Activities to have a public no-arg constructor. Non-static inner classes can't be created without an instance of the outer class, and the Android framework doesn't have (and very much shouldn't have) a way to instantiate a Fragment for the purposes of instantiating an Activity.
Related
There is an asynckTask and 2 methods,which are being called by 2 activities.
i Want to keep the AsyncTask class and the methods inside myApplication class
http://developer.android.com/reference/android/app/Application.html
( which was needed anyway,had some states of app to be maintained).
One other way is to have those methods in each activity and the asyncTask as independent class.
what is the best way?
How about having a base activity class for that?
Something like:
public class BaseActivity extends Activity {
protected void myMethod() {
// do what ever
}
}
Then just extend this BaseActivity to have that method in your activities.
I am using a singleton class to store global variables for the entire project. Also, to host some common functions which several classes/Activities may use, such as launching an alertBuilder window. But in order to do that... I need my singleton to extend Activity like this:
public class dataBaseObject extends Activity {
I tried to extend application, but that won't allow me to do this:
View view = context.getLayoutInflater().inflate(layoutType, null);
therefore, can someone tell me if there are any hidden pitfalls of extending Activity for a singleton ?
It doesn't make sense for an Activity class to be a singleton, because instances of Activity are instantiated by the android system.
What you can do is make an abstract class that extends Activity, like this
public abstract class AbstractActivity extends Activity {
public static final int EXAMPLE_CONSTANT = 345;
public final void exampleMethod() {
...
}
// This may not be needed
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
....
}
}
Then you can make all of your activity classes extend AbstractActivity. You do not need to declare an abstract class like this in manifest.xml.
An alternative solution is to make all of your utility methods have a parameter that is an Activity or a Context and pass this to these methods.
The use of this tutorial is very clear to me however I am using the Actionbar.tablistner interface. How can I pass variables(strings) from fragment to fragment using this interface?
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
}
Create an interface with a method which accepts any variable (e.g. String). Something like this:
public interface TabClickedListener {
public void passParam(String var);
}
Implement this interface in your Activity. From the Actionbar.tablistener onTabSelected() method call the above interface method (on the activity instance you have) passing whatever value you would like. Once you receive this value in your Activity you can pass this to a different Fragment.
I have a class like this:
class TopicFragment extends Fragment{
public TopicFragment(VO vO) {
// some code
}
}
Users are reporting that the app is crashing and the log says this:
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.aaa.wert.TopicFragment: make sure class name exists, is public, and has an empty constructor that is public
I have looked into this links but i am not able to solve this;
Do fragments really need an empty constructor?
Fragment - InstantiationException: no empty Constructor -> Google Maps v2?
Please help me with this.
Just simply add an empty constructor with no parameters which is public. if you have the fragment set in XML without an empty constructor it cannot be created.
public TopicFragment() {
}
I also usually always have just the empty constructor and have a method like this to instantiate with arguments
public static TopicFragment newInstance(VO vo) {
TopicFragment fragment = new TopicFragment();
fragment.setVo(vo);
return fragment;
}
EDIT: and as the guys said in the comments make your class:
public class TopicFragment {
}
I am basically creating a base class that will overwrite (extend) classes such as Activity or FragmentActivity and add custom functionality by defining methods that should be available to any other class, that extends this base class.
The structure if the following, basically:
CustomActivity (extends)->BaseActivity (extends)->Activity
or
CustomActivity (extends)->BaseFragmentActivity (extends)->FragmentActivity
The base class in this situation is called hmFragmentActivity and it extends FragmentActivity. I've defined a custom method inside hmFragmentActivity:
public String pref(String key, String defaultVal) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
return prefs.getString(key, defaultVal);
}
Basically it's a helper method, which I want available in all other activities, by making them extend hmFragmentActivity instead of FragmentActivity.
However, when I try to call prefs() from any activity, it gets highlighted as an error in the IDE.
Here's an example:
public class FuelEconomy extends hmFragmentActivity {
// In some method:
if(pref("fuel_usage_liter", "")==""){
Log.d("fuel", "Fuel set");
}
}
When I highlight pref() above, the IDE says:
Cannot make a static reference to the non-static method pref(String)
from the type hmFragmentActivity