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?
Related
I get intent for push or custom scheme in A activity.
I'm handling them in onResume.
It seems the intents are not disposed unless explicitly told.
I start an activity for intents, and when I close the started activity, it keeps restarted (I suspect this is due to the living intents)
How should I dispose them?
Are there better way of handling for example, push intent?
(without the need to disposing them?)
--- edit
I think my problem is due to the way I handle push, (or scheme)
GcmIntentService creates notification which would start a MainActivity
MainActivity then look at args and starts appropriate activities.
I guess the more conventional way is to go from GcmIntentService to appropriate activities directly?
Try putting this inside onCreate() method of your MainActivity.java :
if (savedInstanceState == null) {
final Intent launchIntent = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(launchIntent);
}
If the activity is already created, rather put this inside onStart() method.
In Android 2.3.3, how does one differentiate Intents that start a particular Activity. For example, if both Activity_A and Activity_B have intents that call startActivityForResult( intent, requestCode), how does Activity_C differentiate between which Activity has started it? Also, I know that one passes a requestCode to the starting Activity, but how does this Activity handle the requestCode? There is no method in Intent that says getRequestCode(). Is the only way to do this to place the requestCode in a Bundle in addition to the method startActivityForResult? Thanks!
Intent API:
http://developer.android.com/reference/android/content/Intent.html
One solution would be to pass along an extra piece of identifying data. For example:
intent.putExtra("activity", "com.whatever.MyActivity");
Then the receiving Activity can read it:
Bundle extras = getIntent().getExtras();
String activityName = extras.getString("activity");
It seems like there should be an easy method call to tell what the sending Intent was, but if so, I'm not aware of it.
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.
I was wondering, how would I make a switch statement, that when that certain case was triggered, it would open a new screen with text. Would I use an intent? And if so, which one?
Thank you for your help in advance.
When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
this will explicitly attempt to open a new Activity with the class name MySecondActivity
to pass a String of text from one Activity to another in this way, you can add it to the intent.
String someValue = "Some Value";
intent.putExtra("Some Key", someValue);
and in the code of your other Activity, you can get at this string via the Intent
getIntent().getStringExtra("Some Key");
Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.
Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.
The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.
I've been reading the sample code from the dev docs on Android's site, specifically this:
http://developer.android.com/resources/samples/SampleSyncAdapter/src/com/example/android/samplesync/authenticator/AuthenticatorActivity.html
Which is the sole activity of the sample app. It refers to an intent in the onCreate method. I don't understand where this intent is coming from, or what it should contain if this is the only activity the app utilizes.
Log.i(TAG, "loading data from Intent");
final Intent intent = getIntent();
mUsername = intent.getStringExtra(PARAM_USERNAME);
mAuthtokenType = intent.getStringExtra(PARAM_AUTHTOKEN_TYPE);
mRequestNewAccount = mUsername == null;
mConfirmCredentials = intent.getBooleanExtra(PARAM_CONFIRM_CREDENTIALS, false);
That's the block of code working with the intent. Why would you have an intent for the only activity in the app? Is this app called in an unusual way? The Manifest does not include an intent filter for the activity... I guess I'm just a bit lost on this whole thing! If someone could set me straight that'd be great, thanks.
Why would you have an intent for the only activity in the app?
getIntent() gets you the intent that started this activity.
Is this app called in an unusual way?
I guess this activity is called programmatically from another app or activity, since it has been passed some extra data: getStringExtra() is used to extract some data from the intent that started it. putExtra.. and getExtra.. is a way to pass data between activities when they are started.
In that specific example, the intent is sent from the addAccount method in Authenticator.java. That method is called by the OS when you click the Add Account button in the Accounts & sync settings screen and choose your account type.