Android Context used in classes other than Activity - android

In my Mainactivity:
LoginUser.loginUser(username.getText().toString(),password.getText().toString(), getApplication());
So in my LoginUser class,
I want to start a dialog box like this:
new AlertDialog.Builder(context).set.....
but failed,
get fault info like this:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Also I would like to use like
Intent intent = new Intent(context, ABC.class)
context.startActivity(intent);
Also failed. And get fault info like this:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I am so confused about all of these, can anyone help me? Thank you very much!

I'm not fully sure of what you're up to, but for the first problem, it looks like you're trying to use a non-activity context to show a dialog.
TL;DR, you cannot an AlertDialog with an application context, it requires an Activity.
Consider something like:
new AlertDialog.Builder(<activity>)
The second problem is also similar, you can start an activity with an application context, but you need to start it as a new task. To do that, you need to add a flag. (Although, this is not considered to be good practice)
Intent intent = new Intent(context, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Check here for more about what you can and cannot do with various types of contexts in Android.

Try this.
LoginUser.loginUser(username.getText().toString(),password.getText().toString(), Mainactivity.this);
replace getApllicationcontext() to your Activity.
new AlertDialog.Builder(<activity>)

You can use this as well
LoginUser.loginUser(username.getText().toString(),password.getText().toString(), this);
LoginUser.java
public void loginUser(Context context){
}

Related

How to start an Intent in Android

I just following a tutorial to start a basic intent, and I get an error. Doing exact the same as the tutorial, created an empty Activity and just passed as argument to the intent. What is the problem?
Using a video tutorial: Lynda.com - Developing Android Apps Essential Training (2015)
The error you've got in IDE usually happens when you try to use Activity Context (keyword this) inside some callback, listener or anonymous function. In such situation this does not refer to the Context of the Activity.
That's why solution provided by #Vishal Patoliya should fix your problem because you're explicitly referring to the Context of the concrete Activity as follows:
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
Try this
Intent intent = new Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
In case of Activtiy, you are going to another activity:--
Intent intent = new
Intent(YourActivity.this,ItemUserSettingRattingActivity.class);
startActivity(intent);
If you are passing intent from a fragment to open a activity then
Intent intent = new Intent(getActivity(),ItemUserSettingRattingActivity.class);
startActivity(intent);
Have you already created a class named ItemUserSettingRattingActivity.java? maybe you get an error it's because the intent you created didn't detect the class.

Start multilple activity from broadcastReceiver

I want to start multiple activities from my broadcast receiver. I have two classes i.e ReadContacts and CallDetails. I want to start them one by one. like first calldetails activity should be started and then next. I have tried below code and it works fine.
Intent calldetails = new Intent();
calldetails.setClassName("com.simplereader", "com.simplereader.Calldetails");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calldetails);
then I tried below code to start other activity
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(readcontacts);
But its not working and application crashes.
You must have the Intent Flag Intent.FLAG_ACTIVITY_NEW_TASK to start an Activity from outside of an Activity context so you need to add that flag to your second Intent.
I don't know if this is your only problem but if that doesn't fix it then post your logcat so we can see the error.
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // you need this flag
context.startActivity(readcontacts);
FLAG_ACTIVITY_MULTIPLE_TASK Do not use this flag unless you are implementing your own top-level application launcher.
From the android developer documentation for intent.
You could probably just launch both activities with the new task flag.
I think you are making mistake in this line
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
If you want to start readcontacts activity it shoul be
readcontacts.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instead of
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
I think this is the reason.

Get calling activity from some other activity

I am calling an activity from another activity by this code:
Intent intent = new Intent(context, mClass);
context.startActivity(intent);
and from my new activity which is started by this code. I want to know which activity starts this activity. I used this code for this purpose
Intent intent = getIntent();
Class<?> c = intent.getClass();
if (c != OffersActivity.class) {
prepareForNotifications();
setListAdapter();
}
but by this code I am not able to get the classname which starts this activity. I really need some help.
thanks
There is a method getCallingActivity(), but that only works, if the calling activity calls you with startActivityForResult(). I have seen libraries use that and say that you must call them that way, but frankly it is a bit nasty. The simple answer is that you're not supposed to know. Android is a system of loosely coupled activities and the calling activity should thus have no real meaning to your activity. Any information your activity needs should be put explicitly in the intent, so if you really want to know who called you then the caller needs to put that extra in. You won't be able to tell if they are lying of course, so don't go trying to use this for anything security-related. However, I would suggest that whatever you're determining based on the caller, is what the caller should be passing you instead. Might be time for a rethink on why you want to do this, since trying to fight the system will only lead to pain.
I would suggest:
public static final String INTENTSENDER = "sender";
void mMethod() {
Intent intent = new Intent(context, mClass);
intent.putExtra(INTENTSENDER, mClass);
context.startActivity(intent);
}
knowing who sent it:
Class<?> c = (Class<?>)intent.getExtras().get(INTENTSENDER);
However, you can also use this:
ComponentName componentName = this.getCallingActivity();
Now, you can use componentName to get the sender of the intent.
Maybe it's best to use the extras parameters in the intent when you call them...like: Intent.putExtra(PARAM,value) on the caller activity...and on the opened activity you check:
intent.getStringExtra(PARAM)
getParentActivity() is not what yout are looking for?

Use of Context to start another Activity

To start an Activity you need an Intent, like:
Intent i = new Intent(context, class)
So to fill in the context parameter, a couple of options are available:
Use MyActivity.this or just this
Use getApplicationContext()
Use getBaseContext()
And I'm sure there are one or two more options.
These options all appear in some sort of tutorial, one uses the first, the next uses the third option.
So which one should I use? Does it even matter? Is it different for different cases?
Yes its different for different cases,
It depends on the scope. Suppose if you are creating a method in a global class that extends Application to create a Toast that is used in every class of your Application you can use getApplicationContext() to create it.
If you want to create a view that is restricted to that particular Activity you can use Activity.this
Also if you want to create an AlertDialog in some inner class say AsyncTask, then you have to use Activity.this, because the AlertDialog is to be linked to Activity itself.
Also don't use getBaseContext() just use the Context that you are having. For getting further information for the same you can see this Answer.
So, the answer to the real question is better to use Activity.this to start a new Activity.
Intent intent = new Intent(Current_Activity.this, Calling.class);
startActivity(intent);
They are different for sure. These are different contexts, and should be used with the least possible scope(context).
For example if we can use Activity's Context instead of ApplicationContext, one should use the activity context, same applies to application context, and base context.
You do it like this....
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);

Android & Robotium - Test activity that expects an extra?

It seems to me that robotium was designed in a way to test 1 Activity at a time instead of the whole application.
So my question is how do I test an activity that expects an extra to be passed to it?
by extra I mean intent.putExtra("Something", object);
The method setActivityIntent(Intent) should be what you are looking for. I used this method to provide a custom Intent to my Activity's TestCase. Just use it after you call super in your constructor.
Intent i = new Intent();
i.putExtra("myExtra", "anyValue");
setActivityIntent(i);
You don't have to do it in the constructor i think, but you need to make sure that you call it before you call getActivity() for the first time. getActivity will use your Intent to create the Activity.
You could override getActivity() instead.
#Override
public NewActivity getActivity() {
Intent intent = new Intent();
intent.putExtra("exampleExtra", "some data");
setActivityIntent(intent);
return super.getActivity();
}
See Testing for Android with Robotium for more details.

Categories

Resources