Unable to get Application Context using SherlockFragmentActivity - android

My problem is similar to this question Can't make static reference to non-static method ( Android getApplicationContext() )
I need to get the context of the SherlockFragmentActivity in order to access the database class. I tried the solution in this link above, but it did not work.
Question 1: How do I get the context in the code below.
Question 2: I get an error that forces me to use 'static' instead of public for the application context variable. I know that static is for a variable that does not change. However, this variable will change each time a tab is clicked on. Also, 'static' variables are not required for the database class. I'm confused as to why I need a static variable here.
my SherlockFragmentActivity:
public class FragmentTabs extends SherlockFragmentActivity {
TabHost mTabHost;
TabManager mTabManager;
static FragmentTabs appState;
TabSwitchIdDatabase tsid = new TabSwitchIdDatabase(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); // Used for theme switching in samples
super.onCreate(savedInstanceState);
appState = ((FragmentTabs)getApplicationContext());
//.... more code
}
public static class TabManager implements TabHost.OnTabChangeListener {..// see code snipit below....}
}
Where i need to put the context
public static class TabManager implements TabHost.OnTabChangeListener {
//... more code
static class DummyTabFactory implements TabHost.TabContentFactory {
//... more code
#Override
public void onTabChanged(String tabId) {
TabInfo newTab = mTabs.get(tabId);
System.out.println(tabId);
tsid.open();// broken , scoping problem
Boolean x =tsid.tabExists(0);
String tabIDfromDatabase = tsid.getTab(0);// broken , scoping problem
tsid.close();// broken , scoping problem
}
}
}

Do you have a constructor for your DummyTabFactory?
Pass the context as an argument to it.
Assign the passed context to a local variable.
So your code should look like something like this:
public class FragmentTabs extends SherlockFragmentActivity {
DummyTabFactory mDummyTabFactory = new DummyTabFactory(getApplicationContext());
static class DummyTabFactory implements TabHost.TabContentFactory {
private Context mContext;
public DummyTabFactory(Context context) {
super(fm);
mContext = context;
}
}
}
Now you can use mContext to access your app's resources.

Are you sure the problem is related to the SherlockFragmentActivity itself?
Have you checked (for example) that you have specified android:name=".MyApplication" in your AndroidManifest.xml file?

Related

Call method(ProjectActivity) from another Library Activity

I want to call one method from library but the method is written in Activity of project how can i achieve this:
Activity.java from (myProject):
public class DataBaseHelper extends SQLiteOpenHelper {
public void favorite(String Str[], Context cxt) {
}
}
Library Activity(Modules(LibraryFile))
public class MuPDFActivity extends Activity{
//Here I want to call( favorite(String Str[], Context cxt)) this method.
}
Please anyone help me with this issue.

is using "this" a form of polymorphism?

i have a class called LauncherActivity that extends FragmentActivity as shown:
public class LauncherActivity extends FragmentActivity {
public Context mContext;
private Common mApp;
public Activity mActivity;
mContext = this;
mActivity = this;
mApp = (Common) mContext.getApplicationContext();
i have read about polymorphism and gotten to know the main use is to use parent references to access child objects.Since LaucherActivity is the child class and FragmentActivity is the parent class,and FragmentActivity is a child class of the Context class in reference to the android documentation, using this as shown below is allowed ? is it a form of polymorphism?:
mContext = this;
mActivity = this;
am presume this refers to the current object which is LauncherActivity ?
Usually , this keyword is uses to say that , refers to the context of the current activity.

How to get the current Activity context for a non-activity class, statically

I have a non-activity class that needs to launch a new activity. Right now I just pass the current context as a parameter, but I would like to access the context statically if possible. I've already tried creating a new application (MyApplication extends Application), both as a new application and the main application, and neither worked. Any suggestions?
Current Code:
public class SharedFunctions {
public static void doSomething(Context context){
Intent i = new Intent(context, NextActivity.class);
context.startActivity(i);
}
}
The cleaner way to do it is to pass in a Context to each method. It's more typing, but it helps to make sure you're not leaking the reference.
Of course, if you really need static reference to it, you can just keep a static member in your SharedFunctions class and set it for each Activity.
onResume() and onPause() may be good places to set/clear it, but depending on your needs, you might want to change it. Just try not to keep references to old Activities.
public class SharedFunctions{
private static Context context;
public static void setContext(Context ctx){
context = ctx;
}
public static void doSomething(){
context.someContextFunction();
}
}
In each Activity:
protected void onResume(){
SharedFunctions.setContext(this);
}
protected void onPause(){
SharedFunctions.setContext(null);
}
create this class:
public class MyApplication
extends Application
{
private static Context context;
#Override
public void onCreate()
{
super.onCreate();
context = getApplicationContext();
}
public static Context getContext()
{
return context;
}
}
after that you must add this class to field name in application (Manifest)
<application
android:name="yourPackageName.MyApplication"
........
</application >
As a result you can call MyApplication.getContext() anywhere in your application and get the context.
hope, I help you.

In Android, is there a more elegant way to retrieve application context inside a inner class?

In Android, is there a more elegant way to retrieve application context inside a inner class rather than passing context as a parameter?
public class MainActivity extends Activity {
class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener
{
private Context context;
private TextView distanceTextView;
public SeekBarChangeListener(Context context, TextView distanceTextView) {
this.context = context;
this.distanceTextView = distanceTextView;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Seeing as its an inner class of an Activity, you could use:
this.context = MainActivity.this.getApplicationContext();
call MainActivity.this from the inner class and it will give you the Activity context object.
You should not use the applicationContext i.e. calling getApplicationContext() unless you really need it, but from your example code you have, Activity context should be enough

Extend Activity and ActivityGroup

I'm writing an application in which i have a set of code which i want to be available in all of my Activities and ActivityGroups. However, to achieve this, I have extended my activities as:
//custom Activity
public abstract class BaseActivity extends Activity
//custom ActivityGroup
public abstract class BaseActivityGroup extends ActivityGroup
//implemented activities in my app
public class PickUser extends BaseActivity
//and
public class Home extends BaseActivityGroup
Now the thing is, whatever the custom code i write in BaseActivity, I have to write the same in BaseActivityGroup too (as in current implementation). This is prone to code-sync problems and i believe not a good technique.
So, how can i make my extensions in such a way that I only write custom code in BaseActivity and my BaseActivityGroup extends ActivityGroup - which is conceived from BaseActivity class?
If i observe how android does this, so the ActivityGroup in android extends Activity class. And I also want to write my custom ActivityGroup class (known as BaseActivityGroup) that actually extends BaseActivity (which is an extended Activity).
Any ideas/suggestions?
First of all ActivityGroups are bad and should not be used. They are deprecated and it is preferred to use a single activity with multiple fragments.
If you must use an activitygroup you are probably best of by implementing a delegate pattern.
Create a delegate that handles all the common methods such as onCreate, onResume and use that in the bases. In this example I save a reference to the activity in the delegate. This circular referencing might not be the pretties. An alternative is to pass on the activity to the methods in the delegate.
public class ActivityDelegate() {
private Activity mActivity;
public ActivityDelegate(final Activity activity) {
mActivity = activity;
}
public void onCreate(final Bundle savedInstanceState) {
// Do stuff.
}
}
public abstract class BaseActivity extends Activity {
private ActivityDelegate mDelegate = new ActivityDelegate(this);
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
}
...
}
public abstract class BaseActivityGroup extends ActivityGroup {
private ActivityDelegate mDelegate = new ActivityDelegate(this);
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
}
...
}
Add an extra final class, called Base.
This one will only contain methods to be called by the other Base classes, such as for instance:
public static boolean createOptionsMenu(final Menu menu,
final MenuInflater inflater) {
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Then, in your BaseActivity and BaseActivityGroup classes, you would call:
#Override
public final boolean onCreateOptionsMenu(final Menu menu) {
return Base.createOptionsMenu(menu, getMenuInflater());
}
Hope it helps!
Just Extend everything to BaseActivity including BaseGroupActivity as everything is a child of Activity in android
you can put your login in a separate file under a method. now call the same method from both BaseActivity and BaseActivityGroup if you need activity instance in file . pass context through constructor

Categories

Resources