Android Context from another class

android get application context from anywhere
android get context from class
android get context outside of activity
android get context from activity
different types of context in android
android getapplicationcontext
how to pass context in android
android getcontext

What I'm trying to do is the following...

FileInputStream fIn;
try {
fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
BufferedReader br = new BufferedReader(isr);
isTrue = Boolean.parseBoolean(br.readLine());
Log.i("IN", "isTrue = " + isTrue);
}

But this is only going to work in the class that extends the Activity class within Android.

I have a "Settings" class which writes and reads the current games settings from a file, but this file has a lot of data I dont really want manipulated.

I was initially using a BufferedReader & BufferedWriter but I cannot set the data to Private which means anyone can just edit the file. With a OutputStreamWriter it is a little more secure at least

How do I get my excising "Settings" class (which has entirely static methods) to have access to the Context so I may use methods such as openFileInput

Create constructor for your Settings class that has Context argument. Then when you instantiate the object from that class, just pass the your application context, thats it.

Constructor:

 public Settings(Context cont)
{
     //do something with the context, e.g assign it to some private variable of type Context

}

In your activity class:

Settings settings = new Settings(MyActivity.this);

How to use "Context" of one class into another class, As there are different types of context in Android, we as an Android Both the Activity and Application classes extend the Context class. Some are saying to pass the context to the class and save a reference to that context like: class YourNonContextClass { private Context context; public YourNonContextClass (Context context){ this. context = context; } public boolean isAirplaneModeOn() { return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) != 0; } }

Instead of passing context, it would be more appropriate to use Fileclass. You should avoid passing context to other classes whenever possible.

    File file = new File("sample.txt");
    InputStream is = new FileInputStream(file);

Another alternative could be to pass the context to the method.

boolean isTrue = Settings.readBoolean(MyClass.this);

Understanding Context In Android Application, onCreate(); appContext = getApplicationContext(); /* If you has other classes that need context object to initialize when application is created,� In the Android SDK, the class android.app.Activity is a subclass of android.content.Context. An activity has all the things that any context has — namely, the app’s package name, the theme, the wallpaper, the names of files associated with the app, and pointers to location services, to user account services, and other info.

If you are using multiple fragments with multiple activities than there is a shortcut method for getting the context. create a static class. define the context. whenever you are changing activity change the context. get the context in fragment using that static class. I know you can get the context in a fragment by getactivity() but if you are using some adapter or noncontext class this will be really helpful

Android Get Application Context From Anywhere Example, public abstract class Context raised based on whether the activity is visible to the user, regardless whether another flag is used to reduce the� Both the Activity and Application classes extend the Context class. Context is almost everywhere in Android Development and it is the most important thing in Android Development, so we must understand to use it correctly. Wrong use of Context can easily lead to memory leaks in an android application.

Context, My mission for this article is to help you master Android Context. This is one They can also choose to expose the components only to other UI Context is Context + Theming, and technically any class which is a subclass of� in another class called the GameRunner like so: public class GameRunner extends Activity { Line line = new Line(new ContextPasser().context); public void draw(Canvas canvas) { line.draw(canvas); getWindow().getDecorView().setBackgroundColor(Color.BLACK); } public void update() { line.update(); } }

Mastering Android context, In other simple words: It's a class that inherits another Context. So, How to use the ContextWrapper? Let's talk about the function we discussed� Create class object of another class and access method in Activity programmatically. Its very easy to create and call function through another java programming class because there android programming is supports OOP( OBJECT ORIENTED PROGRAMMING ) concepts so you can easily create anther class file and access its methods by using objects of that particular class file.

Android Context in non-activity Class, Intent intent = new Intent(context, MyActivity.class); startActivity(intent); In this way, the activity can have a different set of themes or styles than the rest of the� The Context parameter is used first because the Activity class is a subclass of Context. The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start. The putExtra () method adds the value of EditText to the intent. An Intent can carry data types as key-value pairs called extras.

Comments
  • His Settings class has only static methods.