I have the following code in which I am using the application context to retrieve needed information:
public class Data{
private boolean VarA;
public void setVarA(boolean B,Context ctx)
{
SharedPreferences CoreDataStorage = ctx.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = CoreDataStorage.edit();
editor.putBoolean("PrefVarA", VarA);
edit.commit();
}
}
Now I am calling the public method setVarA() from the below class
public class MyActivity extends Activity{
Data cd = new Data();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
cd.setVarA(true,this);
}
}
In the activity above it shows me compilation error that it can't cast from MyActivity to Context. Please suggest any solution. Is the above code is not proper way to pass the context?
You need the application Context to access the shared preferences. It should be:
cd.setVarA(true,this.getApplicationContext());
in the onCreate of MyActivity.
Related
When I need a callback from Activity B back to Activity A I usually make the reference variable in Activity B 'static'. I realize that if the user rotates the device the Life Cycle methods will remove my reference.
Is this the only drawback and is there a better way to register without a static reference. Is it better to simply put all data in the Application class ? - Thank you.
public class MainActivity extends Activity implements InterfaceMainActivityTwo {
static Main2Activity main2Activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main2Activity = new Main2Activity();
main2Activity.setDataListener(this);
}
#Override
public void getDataMainActivityTwo(String string) {
tvTextData.setText(string);
}
}
public class Main2Activity extends Activity {
static InterfaceMainActivityTwo mGetDataInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void getDataSaveBtn(View v) {
if (mGetDataInterface != null)
mGetDataInterface.getDataMainActivityTwo(fullName);
else
Toast.makeText(this, "IS NULL.INTERFACE NOT INITIALIZED !!!!", Toast.LENGTH_SHORT).show();
}
/////////// interface setup
interface InterfaceMainActivityTwo {
void getDataMainActivityTwo(String string);
}
public void setDataListener(InterfaceMainActivityTwo listener) {
this.mGetDataInterface = listener;
}
}
You should never need a callback between two activities. You're doing something wrong if you do. If you need to pass data from A to B, pass it in the bundle. If you need to pass it back from B to A, use startActivityForResult and pass it in the result. If you need to share data between many activities, it should be held in some globally accessible data structure, either in memory or on disk.
How can I use findViewById() in a non activity class. Below is my code snippet. I get the error message: "can't resolve method findViewById" if used directly. And if i try to use the class constructor (Where the imageView is available) i get this error "cannot resolve symbol context"
public class MyBroadcastReceiver extends FirstBroadcastReceiver {
Activity activity;
public MyBroadcastReceiver(Context context, Activity activity){
this.context=context; // error here(cannot resolve symbol context)
this.activity=activity;
}
#Override
protected void (Context context) {
// content
}
#Override
public void onButton(Context context, boolean isClick) {
if(isClick) {
ImageView blueImage = (ImageView)activity.findViewById(R.id.imageView);
blueImage.setColorFilter(0xff000000);
}
}
.......
....
// and so on
And below is my MainActivity with MybroadcastReceiver class instance.Is it correct?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// and so on
}
}
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver(MainActivity.this,this);
#Override
public void onActivityResult() {
// some code
}
#Override
public void onInitialized(MyManager manager){
// some code
}
A BroacastReceiver runs entirely in the background, listening for Intents sent either by the OS or other apps. It is not responsible for any UI interactions, and cannot access any views. Therefore, findViewById cannot be used within a BroadcastReceiver.
See also - What is BroadcastReceiver and when we use it?
You have to pass View to the non activity class, before using findViewByid
and
try using
view.findViewByid(R.id.view_id);
Because context is null in Broadcast class. use Broadcast class constructor to pass parent_activity(Where the imageView is available) context in Broadcast to access the context:
public class Broadcast extends BroadCastReceiver {
Activity activity;
public Broadcast(Context context,Activity activity){
this.context=context;
this.activity=activity;
}
.......
....... //so on
and in parent_activity create Broadcast class instance by passing parent_activity context as:
Broadcast broadcast = new Broadcast(parent_activity.this,this);
Use activity instance as:
#Override
public void onButton(Context context, boolean isClick) {
if(isClick) {
ImageView blueImage = (ImageView) activity.findViewById(R.id.imageView); //<--- here
}
}
.........
......... //so on
I have a value in an activity class. I want to use that value in a non activity class. Normally, to share data between activity classes, I use like,
FirstActivityClass.java
SharedPreferences notification_id = getSharedPreferences("NOTIFICATION_ID", MODE_PRIVATE);
SharedPreferences.Editor notificationIDEditor = notification_id.edit();
notificationIDEditor.putString("notification_id", notificationId)
notificationIDEditor.apply();
And to retrieve the value of notification_id in another class,
SecondActivityClass.java
SharedPreferences notificationIDSharedRetrieve = getSharedPreferences("NOTIFICATION_ID", MODE_PRIVATE);
notificationID = notificationIDSharedRetrieve .getString("notification_id", null);
But suppose the second class was a non-activity class, how can I retrieve the data in a non-activity class?
you can send your Activity context to your calss by creating a custom constructor for example:
class A
{
Context con;
public A(Context con)
{
this.con=con
}
}
Activity B
{
Context con;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.con=getContext();
A = new A(this.con);
}
}
You can cache the global Application context.
myApplicationContext.getSharedPreferences(NOTIFICATION_ID", MODE_PRIVATE)
I want to call a method from a android library class which i have imported as a androidlib.jar. As i am able to call a whole class of library but i dont want it, but i want to call a particular method of library class.
I tried something like this, but it is showing java.lang.Nullpointer exception
This is my library class (AndroidLiB.class), where i have imported its jar file
public class AndroidLiB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.android_li_b);
startGPS();
}
public void startGPS()
{
Toast.makeText(getApplicationContext(),"Your GPS started",Toast.LENGTH_LONG).show();
}
}
This is my application class where i want to call a method from above class
public class AndLib1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.and_lib1);
AndroidLiB abc = new AndroidLiB();
abc.startGPS();
}
}
But it is not working
If you want simply a method from jar, then why you need to extends Activity. My suggestion is remove extends Activity will fix the NPE error.
Try this,
public class AndroidLiB {
Activity activity;
AndroidLiB(Activity activity){
this.activity = activity;
}
public void startGPS()
{
Toast.makeText(activity,"Your GPS started",Toast.LENGTH_LONG).show();
}
}
And In your main class call like
AndroidLiB lib = new AndroidLiB (this);
lib.startGPS();
I would do something like this:
public class Tool {
private Tool() {
// no direct instantiation
}
public static void startGPS(final Context context) {
Toast.makeText(context, "Your GPS started", Toast.LENGTH_LONG).show();
}
}
then
public class AndroidLiB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_li_b);
Tool.startGPS(this);
}
}
and
public class AndLib1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.and_lib1);
Tool.startGPS(this);
}
}
You can extend library class. For example:
public class YourActivity extends AndroidLib
{
super.onCreate(savedInstanceState);
setContentView(R.layout.and_lib1);
startGPS();
}
What you're trying to do isn't really the best way to do things, but I'm assuming that the question being asked is how to import the jar correctly. If so:
If using eclipse,
Make sure the androidlib.jar file is in the libs folder.
Right click on androidlib.jar and select Build Path > Add to Build Path
Right click on your project folder, go to Properties > Java Build Path > Order and Export and then make sure androidlib.jar is selected.
The problem with your current code is that when you call getApplicationContext(), the Activity hasn't been started yet, therefore there is no context. A quick and dirty solution would be to rewrite the startGPS() method like this:
public static void startGps(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
But I would much rather put that method inside some sort of Utilities class or even inside a parent Activity class.
I have a Activity class from where I am passing some information to a helper class(Non-activity) class. In the helper class I want to use the getSharedPreferences(). But I am unable to use it as it requires the activity context.
here is my code:
class myActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Info = new Authenticate().execute(ContentString).get();
ItemsStore.SetItems(Info);
}
}
class ItemsStore
{
public void SetItems(Information info)
{
SharedPreferences localSettings = mContext.getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localSettings.edit();
editor.putString("Url", info.Url);
editor.putString("Email", info.Email);
}
}
ANy idea how this can be achieved?
Instead of creating memory leaks (by holding activity context in a class field) you can try this solution because shared preferences do not need activity context but ... any context :) For long living objects you should use ApplicationContext.
Create the application class:
public class MySuperAppApplication extends Application {
private static Application instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static Context getContext() {
return instance.getApplicationContext();
}
}
Register it at manifest
<application
...
android:name=".MySuperAppApplication" >
...
</application>
Then you can do something like this
public void persistItems(Information info) {
Context context = MySuperAppApplication.getContext();
SharedPreferences sharedPreferences = context.getSharedPreferences("urlPersistencePreferences", Context.MODE_PRIVATE);
sharedPreferences.edit()
.putString("Url", info.Url)
.putString("Email", info.Email);
}
Method signature looks better this way because it does not need external context. This can be hide under some interface. You can also use it easily for dependency injection.
HTH
Try this:
class myActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Info = new Authenticate().execute(ContentString).get();
ItemsStore.SetItems(Info, getApplicationContext());
}
}
class ItemsStore
{
public void SetItems(Information info, Context mContext)
{
SharedPreferences localSettings = mContext.getSharedPreferences("FileName",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localSettings.edit();
editor.putString("Url", info.Url);
editor.putString("Email", info.Email);
}
}
You need to pass the context to the constructor of non activity class
ItemsStore itemstore = new ItemStore(myActivity.this);
itemstore.SetItems(Info);
Then
Context mContext;
public ItemsStore (Context context)
{
mContext =context;
}
Now mContext can be used as Activity Context.
Note: Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
Write a public function in your activity. While creating an instance of your helper class in Activity class, pass the context of activity in constructor.
Then from your helper class, using the activity context, call the public function in activity class.