I have to send getApplicationContext to a method, and in this method, I have to cast context to activity to use
getWindowManager().getDefaultDisplay().getMetrics(dm)
But it show an exception
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity.
Why getApplicationContext?because I check my application update in main activity and don't want to limit it to an Activity (I want lifecycle).
How handle it?
UPDATE
I have FN.java that all static functions are here. one method job (getUniqueID) is to get device ID and screen width and height and etc. IN another hand, I have a Update.java that's for checking application update.
In this class, I use getUniqueID.
For using Update class in my main activity, I do it like :
if(updateInstance == null)
updateInstance = new Update(getApplicationContext());
If I pass this instead of getApplicationContext, I know it's ok, but I want use getApplicationContext because maybe user click on some menu item and go to another activity or close application immediately; And I want to show notification Anyway.
I just use
this
and everything works fine.
And for update check use singleton.
Related
I'm facing a problem in starting an activity in Android.
What am i doing?
I have a setOnClickListner in SignUpActivity.class which calls a method called "DoSignUp" located in MethodsParse.class.
What does this "DoSignUp" do?
It literally does the signUp using the values receveid from parameters.
But, as i'm using Parse library and Parse has an exclusive method to do SignUps: "signUpInBackground()", i don't know what to do.
What trouble am i facing?
When i try to do the signUp as an existing User, it works fine (shows me the Toast with: Something went wrong...)!
When i try to do the signUp as a new User, it shows me a error:
But, I tried to debug my app, and when i tried to do the signUp as a new User, the data from it goes to Database, it means, my problem is on startActivity... but i don't know why.
I see some problems
Remove extends Activity from MethodsParse, you use it as a regular Java object, hence it should not extend Activity
Looks like you initialize Parse inside MethodsParse. This should be done in an extends Application object, see: https://parse.com/questions/should-developers-call-parseinitialize-in-oncreate-of-every-activity-android or How to know when Parse.initialize() has already been called?
Looking at your error I strongly believe 1) to be the cause of your error. You call startActivity(..,..) and the method startActivity() is only available because you extend Activity. The problem is, since you call the login method directly from another activity MethodsParse has not started it's lifecycle (by the OS calling onCreate()), making startActivity() fail as it has no context.
Long answer short, change:
startActivity(..,..)
To:
currentCtx.startActivity(..,..)
The exception is nullpointerexception, one of the variable that you are passing is null. This error could be due to nextClass or currentCtx is null.
Trying to call a function from an activity, but I need the specific instance of that activity to do so.
Is there a way to call that specific activity from the application?
If not, is there a way to start an activity from the application so that I always have access to the instance I start running? I tried this, and edited the manifest, but the app never started...
As concerned with the limited details in this question, I think your requirement is to call a function in an activity that needs that activity itself as the parameter. I think you can do it like this.
Activity actiity=this;
yourMethode(activity)
{
//body of your methode
}
Whenever you use the variable "activity", you can get an instance to the current activity.
I think you are talking about casting the context to get the type of activity it represents. You can do it like this. But be careful if the context is not of that type you will most likely cause a crash.
((MainActivity) mContext).myMethod();
This is not really recommended as it will cause some tight coupling between the class and the activity.
can I use activiy one time(register activity) and switch the main launcher after using to different activity?
another question if I may,
If I create parameter x in one of the activities in my application, can I use this parameter in other activities?...If yes, how I can do that?
thanks :)
You cannot dynamically change the launcher activity once it has to be only 1 activity that is defined in the manifest file.
I would recommend having something like a landing or splash activity which checks a shared preference variable, to decide which activity to launch, for example either a login activity or another activity.
You should not access a variable in an activty from another activity, you should store these in data holding classes. however if you want to do it, for a good reason, simply make it static.
You cannot adjust the manifest after running your application. What you can do is have your default launcher activity write to SharedPreferences once it has been run once. Inside of that activity check to see if that preference has been set and if it has just finish that activity and launch your new activity, the user will not see anything if you do this in the onCreate of the launcher activity.
As for passing params between activities you should use intent extras. For example to pass a string use putExtra(String key, String value), and to get that parameter inside of the new activity use getStringExtra("Key").
For global variables accessable from different activities you can also extend Application class and then access it via getApplicationContext().
1. One time activity launch
You can't change the main launcher. It's a static information. What you could do is following:
// in the beginning of onCreate
// first launch could be loaded from shared preferences
// see 2. for more
if (!firstLaunch) {
// start another activity
finish();
return;
}
2. Use data in another activity
One way is to persist the data and load it somewhere else. You will find all information you need in the Data Storage article.
If your data is primitive you could try to pass it by intent to another activity. See Using integer from one class in another Android.
If it is complex you could try to implement an own Application class and use helper methods to access global data. See Android: Accessing resources without an Activity or Context reference.
Be careful with that, please read the Avoiding Memory Leaks article then.
My launch activity starts up another activity whose launch is set to single instance. In this 2nd activity, I have a public method. I then start up a 3rd activity and that activity needs to access the public method in the 2nd activity. I don't want to use startActivity and pass it extras because I assume the onCreate will get called (or am I wrong?) and I need to avoid the 2nd activity from reinitializing itself.
When an activity is started using startActivity, is it possible to gain access to the underlying class instance itself and simply call the method?
I actually came up with a simple solution. As a matter of fact you can access the underlying class of an activity. First, you create a class that is used to hold a public static reference to activity 2. When activity 2 is created, in its onCreate method you store "this" in the static reference. Activity 2 implements an interface with the methods that you want available to any other activity or object. The static reference you hold would be of a data type of this interface. When another activity wants to call a method in this activity, it simply accesses the public static reference and calls the method. This is no hack but is intrinsic to how Java operates and is totally legitimate.
It is not a good idea.
As I can understand method from second activity is actually not connected to particular activity while you want to call it from another one. So carry the method out to other (non-activity) class (maybe static method) and use it from both activities.
It's not directly possible to gain access to activity object started using startActivity (without using some hacks). And frankly you shouldn't even trying to accomplish this.
One Activity component can cycle through several Activity java object while its alive. For example, when user rotates the screen, old object is discarded and new activity object is created. But this is still one Activity component.
From my experience, when you need to do things you described, there is something wrong with your architecture. You either should move part of activity's responsibilities to Service or to ContentProvider, or use Intents, etc. Its hard to recommend anything more specific without knowing more details.
No there is no way to pass a reference via startActivity() however you can use some sort of shared memory to keep reference to your Activity. This is probably a bad design. However passing an extra with your Intent will not cause onCreate, that is completely related to the lifecycle.
I have Activity A which defines a dialog using AlertBuilder.create etc. This Activity invokes the dialog using showDialog(dialogID). dialogID is declared and recognized in all classes/activities. Everything works perfectly in Activity A.
My question is when trying to invoke this same dialog -- showDialog(dialogID) -- from another Activity (Activity B) the application crashes. Can anyone help?
How to invoke a dialog from multiple activities?
Thanks in advance.
Andy
My question is when trying to invoke
this same dialog --
showDialog(dialogID) -- from another
Activity (Activity B) the application
crashes.
How do you invoke the dialog from Activity B? Just to let you know, you do not instiante activities (so no new ActivityA().showDialog(id).
What you can do is
Create a class that extends AlertDialog.Builder and accept a Context parameter in the constructor. You can customize the text, buttons and other things.
From your activity, in your onCreateDialog, you can just instantiate your class and call create() on it. And your class will be accessible from any activities.
I don't think you can accomplish what you want without hooking up a similar entry point in your new activity.
showDialog(int id)
The id is unique within the activity that launches it. So if two activties A and B both call showDialog(1); That will do something different in each unless someone has coded the same code path for them in their onDialogCreate() and onPrepareDialog methods. So in your onDialogCreate of the original activity, that code will have to exist in both activities. You can sometimes get way with creating a new Dialog type that does all the initialization internally based on a given context and just call show() on it. The problem with this solution usually arises when the context is no longer valid and you need to dismiss or show it. Basically though when using showDialog() it's on a per activity basis.
I don't think that's possible.
Dialog built in Activity A, belongs to Activity A.
No matter if you store its ID in a global data space. It won't be available to be used in Activity B.
You'll have to create another Dialog in Activity B