I am writing a test for my activity
MyActivity activity = new MyActivity();
activity.onCreate(null);
my activity expects and extra parameters to be passed, so when I call onCreate it crashes.
How can I pass an extra to my activity without using an Intent?
You could have the activity under test use some default values for the extra parameters, but I don't recommend that approach. Instead, just have the test pass in the values.
From http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html#setActivityIntent(android.content.Intent)
Call this method before the first call to getActivity() to inject a customized Intent into the Activity under test.
Related
I'm just curious to find if there is any difference for starting activity between these two types
StartActivity (typeof (MainActivity));
StartActivity (new Intent (this,typeof (MainActivity)));
Consider the first as a shortcut for the second.
If you dont want to share any data to the new activity you can call the first.
In other cases you may want to pass an Id or any other data to use in the target activity through a Bundle with the PutExtra methods of the Intent.
There are two activity classes in my project and a third class which is subclass of Thread.
Thread Class implements Bluetooth Socket which isn't Parcelable.
First Activity starts the Second Activity using startActivityforResult()
Second Activity creates an object of the Thread class and starts the thread.
I need to pass an object reference of the Thread object from Second Activity to First Activity's onActivityResult() so that I can access Thread object from the first activity.
How can I achieve this?
You have a few options.
You can either break down your object into simple data types and put those values as extras on the intent that you pass back with setResult(), do do so you'd use intent.putExtra(key, value)
Or you can make your data object implement the Parcelable interface so that you can add the data object directly to the intent.
the code to do the latter would look something like this
Intent resultIntent = new Intent();
resultIntent.putExtra("resultObject", mObj);
setResult(ACTION_OK, resultIntent);
then inside your onActivityResult you can pull it out like this:
data.getParcelableExtra("resultObject");
For the latter method to work you need to correctly implement parcelable with your data object. The former method does not require this however, since you'll be passing back simple values only. You'd then have to take those simple values and "re-inflate" the data object on the other side.
I think the best way to achieve this would be to use a singleton. You can only store primitives in Shared Preferences and Bundles. Here is a great reference for creating a singleton.
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
I have one activity 'A' which extends mapActivity which calls service, and one more Service 'B' which extends service. I'am getting Latitude and Longitude in service 'B'. Now i need call the method in Activity 'A' and pass the Latitude and Longitude. In that method I have code to display Location. Thanks in advance..
See http://developer.android.com/reference/android/app/Activity.html#StartingActivities
The A activity should start the B activity using startActivityForResult() method
The B activity should set the data to return (latitude and longitude) with setResult() before doing finish()
The A activity must override onActivityResult() to retrieve data from B
You have to update the activity some service. Class that extends the activity is activity.So in your case B is Service
Try to use runnable and handler to update activity from service
Check here
Use intent to call one to other
Intent i= new Intent (this,B.class);
startActivity(i);
Its an easyway to call
Intents are used for conveying short message between activities . use Intent and put extras for starting activity from service u need to add extra flags also to start activity from service.
here is reference
http://developer.android.com/reference/android/content/Intent.html
You are using one Activity and one Service, not two activities, and you want interaction between activity and service. To do so, use BoundService for the same.
See below link for more reference:
http://developer.android.com/guide/components/bound-services.html
Alternatively you can define A messenger in Activity, and can send message from service to activity.
Create that class object and call the particular method
Using intents is the easiest and the best way. In Activity A have this code to create intent:
Intent intent=new intent(this,B.class);
/After the data which needs to be sent to activity B is Processed/
/If you want to pass data/
intent.putExtra("Data", data);
startActivity(i);
In Activity B recieve it as:
Intent intent = getIntent();
String text = intent.getStringExtra("Data");
You should not create an instance of the activity class. It is wrong. Activity has ui and lifecycle and activity is started by startActivity(intent)
You can use startActivityForResult or you can pass the values from one activity to another using intents and do what is required. But it depends on what you intend to do in the method.
May i know the exact requirement, why you require to call another activity method ??
In my application, there are two activity classes. Suppose A and B. Activity B has a member function name myfun(), Is it possible to call myfun of activity B from activity A with starting activity B. If yes, please provide solution
thanks
You can initiate a object of your ActivityB, and than run the member function. (Use it like an normal object)
E.g
ActivityB act = new ActivityB();
act.yourMethod();
(the onCreate will not run when using the "new" keyword, only the constructor of ActivityB.)
Btw. If the method is not depending on any special state or member variables of the ActivityB class, I sugges you move/refactor the method to another Util class or something like that.
you can create for sure, but you won't able to use that object to start activity, instead you can create a static method, or variable, and can use directly.
public static myFunction(int parameter)
{
// Your code
}
and call this method by
YOurActivtiyClass.myFunction(parameter);
I have an asynch task with my app which goes to a website, grabs the results from the API and appends a number of clickable textviews to an existing LinearLayout.
However I want to be able to launch a new activity when the textview is clicked. This isn't possible with the asynch class defined in a seperate file, would it be easier to define it as an inline class within the activity?
You can always pass Context to your async class.
A better approach would be to have callbacks (listeners) in the calling class for the async to call back to.
One approach is to inflate your TextViews from an XML file that declares an onClick attribute, naming a method defined in your Activity.
Do not use a context as an Activity! You will probably receive a cast error anyway. Instead, you can pass the activity as a function parameter, like this:
public void function(Activity act)
{
Intent intent = new Intent(act, newActivity.class);
act.startActivity(intent);
}
Or overload the constructor to accept the activity as a parameter. But I strongly suggest you to check you code. If you are calling an activity, you, probably, should be within another one, don't you agree? But, I Know that sometimes we have to make a few concessions, in order to make things work properly. So, use it wisely.