Android & Robotium - Test activity that expects an extra? - android

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.

Related

How to run activity from 3rd party library?

I am in a non-activity class. In this place I want to run the activity from a 3rd party library which I attached to my project. I have written some code which I thought will do it:
Intent intent = new Intent();
intent.putExtra(THIRD_PARTY_ACTIVITY.REPORT_EXTRA, parts.toString());
Activity activity = new THIRD_PARTY_ACTIVITY();
activity.startActivity(intent);
This code compiles successful but when I execute it, I get the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
How can I run activity from a 3rd party library?
You're doing it wrong. You NEVER create an Activity with new. It won't initialize correctly. Instead, you create an Intent to launch that Activity and call context.startActivity() to create and launch it.
void launchThirdPartyActivity(Context context) {
Intent intent = new Intent(context, THIRD_PARTY_ACTIVITY.class);
context.startActivity(intent);
}
That should work if the activity is in a library. If you're trying to launch an Activity in another app on the device, you'd use one of the other Intent constructors (which one depends on how/what you're trying to launch- a specific activity in a specific app? An activity that can perform an action (like share or view) on a specific data type? Something else?)
In your library you can do this:
Intent intent = new Intent();
// Here you need to set the ACTION or the COMPONENT in the Intent
// so that Android knows which Activity you want to start
// Also, you need to set FLAG_ACTIVITY_NEW_TASK because you aren't calling
// startActivity() on an Activity Context
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(THIRD_PARTY_ACTIVITY.REPORT_EXTRA, parts.toString());
Application.getApplicationContext().startActivity(intent);

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?

Can we invoke one activity into another activity?

I Mean that i want to use one activity into another activity, Like class using create instance of that class. Is it Possible?
Well, I think you should use Intents to call an activity from another activity.
Call this from your Activity:
Intent in = new Intent(getApplicationContext(), NextActivity.class);
startActivity(in);
you can do it only by saying startActivity(), no other go. you can't make an instance of Activity because , an Activity gets created when its onCreate() method gets called, but when you say new MyActivity() its default constructor is called and not its onCreate() method (which Android OS will not accept). so always say startActivity() or startActivityForResult() which are handled by android OS
Write this code from where you want to run activity
Intent intent = new Intent(current_Activity_name.this,New_Activity_name.class);
startActivity(intent);
And add the following code into manifest file
<activity android:name=".New_activity_name" />
Well, since an Activity is a displayable-window, the appropriate concept would be that one Activity can be "launched" from another. This is how you achieve that:
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
CurrentActivity.this.startActivity(i);
This code snippet can launch NewActivity from any point in the CurrentActivity code, for example, an 'OnClickListener'.
Yes, it is possible. This is achieved through Intents.
Intent intent = new Intent(this.getApplication(), TARGET_ACTIVITY_NAME.class);
//To add data use intent.putExtra(NAME,VALUE);
intent.setData(data.getData());
try
{
startActivity(intent); // This ll launch the TARGET_ACTIVITY_NAME
}
catch(Exception e)
{
}
For more information refer this link.
Shash

Constructors in Android Programming?

I have some values and want to pass with activity so that I can show in TextViews of Activity, I am unable to understand such a concept, so what should I do?
Simply I want to make constructor but unable to understand it that how will it be done, I am new to android programming, so needed help.
Start by reading the documentation at developer.android.com about intents and intent extras.
In android if you launched an activity there is a method called onCreate execute automatically.You can send values using Intent to the activity and can retrieve them in the Activity
The Activity (sub)class must have a default constructor without any parameters so that the system can instanciate it at run time.
To pass "parameters" to activities, you need to use the extra bundle of the intent.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.sample.MyParameter", 666);
startActivity(i);
See Starting An Activity
If you want to pass one value then you can use intent but if you want to pass multiple values then using "Bundle" is best way.
Bundle bundel = new Bundle();
bundel.putStringArray("key1",strings);
bundel.putStringArray("key2",stringsofids);
bundel.putString("key3", str31);
bundel.putStringArray("key4",stringsbakup);
bundel.putString("key5", str1);
bundel.putString("key6", str4);

Difference Between 2 Ways to Start an Activity?

I have seen the following two examples of starting activities in Android:
Example 1
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
Example 2
// Calling activity
NextActivity.show(this)
// In the called activity
static void show(Context context) {
final Intent intent = new Intent(context, NextActivity.class);
context.startActivity(intent);
}
It seems the obvious difference between the two examples is that you attach the logic of how an activity is created to the implementation of the activity. Are there any other key differences? (e.g. is the calling activity told to wait until the called activity finishes in one case, but not in the other, etc.)
I see no difference to your 2 methods, other than the 2 lines of code in your first method just happen to be located in a static method that just happens to be located in the 2nd activity's class.
The actual lines of code that are being executed to start the activity are identical. Thus the behavior of the 2 methods will be identical.
Also, the code could be shortened to
context.startActivity(new Intent (context, NextActivity.class));
Only reason to create an instance of Intent as a field is if you need to set flags or add extras, etc.

Categories

Resources