_alerts = new Intent(homeActivity,Alertss.class);
homeActivity.startActivity(_alerts);
...
...
...
homeActivity.stopService(_alerts);
I need to stop the Activity i created from the object where i created it. I tried it in the way above but it did not work. Can any one suggest me a way to do this.
You can use Activity.startActivityForResult() and then Activity.finishActivity() passing in the same result code you used for starting it.
However, this does sound like something that would be better done just as a Dialog.
Related
My app contains two classes: MainActivity, Activity2.
Activity2 needs to access a non-static method of MainActivity. How to do that?
I'm new with Java and Android, if you can, please explain clearly for beginners what to do.
Thanking you in advance.
Instead of calling methods from a different activity you should use Bundles to pass values from ActivityA to ActivityB when B is started from A.
Alternatively if you want to reuse code you should create a non-activity object which you can create two instances of. Say if you do a lot of heavy calculations in both activities, you can put your calculating code in a "Calculate" object. And just initiate it like you would any other Java object. Just note that these two instance will not share any data between each other.
Calculate calc = new Calculate();
calc.codeIdLikeToReuse(numbersAndStuff);
Hope this helps. I would recommend that you read up on the Activity Life Cycle to get an idea on how the life on an activity is.
Use Broad cast receiver to call methods in different activities you can find help here
and one example
Basically, you can't do that. Two activities don't communicate this way. Usually, only one activity is alive at a time (also this might not be always true). The real answer is to use Intents.
You should read some basic Android tutorial like the anddev book.
you also can use libraries like EventBus to link the code.
refer to this post
I want to pass the WebView from One activity to another Activity.
how to do that. I referred How do I pass an object from one activity to another on Android?
but still not getting any idea for this. Please help me to get this....
Sounds to me like you may want a broadcast receiever...
try looking here: BroadcastReceiver
That will let one activity send a message to another
either u try to use finish() an the passed Activity or you simply call finish() in the activity after it's done with it's calculations
Are you sure you want that in an Android activity, not just a normal Java class?
I am at a point of code where I need to use same activity with different values. Is it possible to use same activity recursively?
Yes, its possible but you have to use the condition inside the activity based on the value pass through intents.
It may possible for the same activity with different values so the value pass from previous activity will decide what data will display..
Yes.you can use the same activity but make sure,you don't go into infinite loop nor you get messed up with different codes for various conditions you call the activity.you can achieve that by maintaining extras in Intent you use to call the activity.
Wouldn't it be more suitable (in terms of readability) to create a new Activity-class with the same layout but other functionality? This way one Activity will have one behaviour instead of two - making your code more simple, readable and maintainable.
I have an activity which has a static method for updating a textfield. This way I can update this view from another activity.
But now I'm trying to get a Context variable in this static method which is not possible. I've tried declaring a Context variable and initialising it in onCreate ( context = getApplicationContext();)
But still I can't access context in this static method. How is this normally done?
edit: a little bit more information about my situation. I'm starting a countdowntimer in an activity(a) which updates another activity's(b) ``textfield every second. And it does this by accessing b's setTextField in a static way..
How is this normally done?
Accessing a TextView via a static method is not the best way to update the field from another activity. If you want to pass a value to the activity when it starts, you can send data via the intent (i.e. intent.getExtras). If you want to pass data back from a sub-activity, you can use startActivityForResult.
The way you are going is very strange. Why are you trying to change one activity content from another? May be you need to use startActivityForResult to strat a new activity and then return result from it and change views depending on it?
You might want to check some documentation on OO and using static functions. It is not considered a very good approach.
But as we are not talking about a better complete sollution: you can add a parameter with a context to the function, and just give it when you call the function :)
I would suggest the LocalBinder pattern to update the other Activity:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
Can you do something like this?
something like this <viewobj>.getContext()
Ref: How can I start an Activity from a non-Activity class?
Whenever you're busy with Activity A, there's no point in updating something on Activity B as it is simply not shown to the user at that point in time.
Seems to me you need to have some kind of global variable here that can be picked up in the onResume of Activity B.
Checkout this question : How to declare global variables in Android?
It shows you how to use the Application class to maintain global application state, accessable from all activities when needed.
I have all of my intents in activity groups so I can navigate easily using tabs. I've now encountered the problem where I need to pass information to another activity and I don't know how to approach because the activity isn't loaded the normal way.
Here is how I call the new activity:
setContentView(view);
Thanks in advance.
Edit: I put up a custom method because I'm stupid. Fixed now.
Not sure what you are saying but if you are just replacing the view you are not going to a new activity so everything should still be in scope and accessible.