Call a specific function every time Main_activity is loaded? - java

I have created an application where in oncreate i am calling a function overlay(), which puts an overlay above the application. Then there are multiple screens that user can browse. My problem is I want when user Goes back to the Main screen that function should be again called and that overlay can be seen.Is there something i can use?

Try adding this code in your mainActivity
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
overlay();
}

Related

OnResume to only happen when activity is accessed the second time

I am trying to write code for my android application for things to happen when the activity is Resumed. Although I only want this stuff to happen when the activity is re-visited for the second time and not the first time.
So I want the user to do what they need in the onCreate method in their first visit to the activity and then when they go back to the activity then the onResume code happens.
Should I be using a different method or how can I achieve what I am looking to do?
public void onResume(){
super.onResume();
RunningStatus = sharedPreferences.getBoolean("RunningStatus", false);
if (isS1Pressed) {
if (isPressed) {
if (RunningStatus = false) {
pause.setBackgroundResource(R.drawable.start);
}
}
}
EDIT:
I don't mean second time I mean every time after. Basically I have a production line and my app determines whether it is ahead or behind. So when the user enters this activity then the can perform the calculation. We have a pause button where the user can pause the line if the production line has been stopped in real life. Although I would like that the user can then go out of that specific production line and do calculations on other lines too. Although at the minute once I leave the activity I lose all its state.
Thanks
You could use a boolean flag. in onResume e.g.
if (secondVisit) {
...
} else secondVisit = true;
If you want to do some stuff for very specific time (only 2nd, not 1st or 3rd, 4th,...), I suggest you should use some counter variable and use activity method onSaveInstanceState() and onRestoreInstanceState() to store and retrieve. If you don't want to reset the counter when app got killed, use data persist like SharePreference to store counter.
i assume that you have gone through the activity life cycle if not then visit
https://developer.android.com/guide/components/activities/activity-lifecycle
boolean secondUse=false;//this should be a field in the activity class
onRestart() method is called only when we visited the activity and and came back and then we visit(i.e. when the activity already in the activity stack).
if you have other use case, then using sharedPreference would be better.
override the onRestart()
inside it write the following
if(!seconduse){
//do your stuff
secondUse=true;
}
as you said in the edit you need the stuff to work on every visit after first visit then simply put your code in onRestart() method
public void onRestart(){
super.onRestart();
RunningStatus = sharedPreferences.getBoolean("RunningStatus", false);
if (isS1Pressed) {
if (isPressed) {
if (RunningStatus = false) {
pause.setBackgroundResource(R.drawable.start);
}
}
}
}

Overiding onResume method in android not working in every fragment

I'm creating android app that have 3 fragments and I want refresh data every time I come back to the fragment. So, I override onResume() method in every fragment and add system out print to onresume to check if it's worked correctly.
But when I navigate to 2nd fragment it shows the add system out print of fragment 3 onresume. when I go to fragment 3 it not showing any add system out print. but when I came back to 2nd again it shows add system out print of fragment 1.
Please help me to fix this issue.
It appears you are using FragmentStatePagerAdapter in your ViewPager. It is the expected behaviour of the adapter that only neighbouring fragments are created. If you do not want this behaviour use FragmentPagerAdapter. But be aware of the memory taken up by all the fragments.
Make your fragments implement an interface:
public interface Listener {
void resume()
}
Make your activity implement OnPageChangeListener:
viewPager.addOnPageChangeListener(this);
and then do the following:
#Override
public void onPageSelected(int position) {
((Listener) mAdapter.getItem(position)).resume();
}

Update TextView from another Activity

I have two Activities in one application.
First one updates its TextViews every 3 seconds. It works fine.
When the keyguard (lock screen) is activated the first activity launches the second activity which appears over the lock screen (in order to show data even if the screen is locked). It also works fine.
I would like the TextViews of the second activity to be updated periodically by the first activity. I have played hours with this and tried a lot of suggestions I found with Google but none of them worked for me. The second activity always crashes with NullPointerException at the moment when the TextView.setText() is called.
What is the best practice for doing this?
Thanks in advance for any help.
I don't think there is a good way to do this, as your first activity could get collected by the system, and you generally don't want to do work after onPause has been called.
I would move that logic that updates the views into a service that runs in the background. Since it sounds like you only need this service while the application is running I would create a bound one.
http://developer.android.com/guide/components/services.html
You can pass the data on calling another activity as :
Intent intent =new Intent(FirstActivity.this, SecondActivity.class);
intent.putStringExtra("TextName","Value");
startActivity(intent);
As Ashish said you could use EventBus.
Add the library to your app and in your Second Activity register your activity in the EventBus in onCreate method:
EventBus.getDefault().register(this);
Create a new class in your project to define an event type:
public class TestEvent {
public TestEvent() {}
}
So in your second activity create a method to receive the event:
public void onEvent(TestEvent event) {
//stuff to do
}
Now, in your first activity you just have to "fire" the event in the method executed each 2 seconds:
EventBus.getDefault().post(new TestEvent());
Each time you execute post method, the onEvent of your second activity will be run.
A way to do it is by defining a Singleton object that holds the value to be displayed on the TextView, for instance, a Integer or a String.
Both activities have access to read/write into this object. So when you come back to the second activity, maybe on the onResume() method..you can the following:
public void onResume() {
super.onResume();
textview.setText(""+ MySingleton.getInstance().getValue());
}
On the other activity:
public void updateMethod() {
int newValue = .....;
MySingleton.getInstance().setValue(newValue);
}
This will make sure that whenever you come back to this activity (as onResume() is called), the value will be updated into the TextView. Of course, assuming that you are updating the value from the other activity accordingly.
Note this is the simplest solution you can do, professionally, I would do an event driven solution, where the observer gets notified when the value is changed. For that you can play with http://square.github.io/otto/ library.

Android Java get if minimized

Is there anyway to check if the application is minimized or if you have locked your device?
Because when I do minimize / lock my device the application is still runnig, this is mainly because I'd like to pause the music / sfx not to annoy people.. Like if someone is calling.
I am using Activity and SurfaceView with threads.
I have tried putting my pause method in the surfaceDestroyed / surfaceChanged but without success.
You should understand the activity lifecycle first
when the activity comes int the foreground it'll enter onPause() and it'll enter onResume if the user returns to the activity, an example to use onPause is like this
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// DO YOUR STUFF HERE
}
}
for furthere reference about onPause you can see it here -> onPause Tutorial

How to refresh android GridView

I'm doing GridView activity that first show default image, then i start a new thread for netowrk task to download image from datatbase, and i would like that after the thread is finished, the GridView will automatically refresh the images in grid.
from this question i took the following code:
ImageAdapter adapt = (ImageAdapter)gridView.getAdapter();
adapt.setBitmap(bitmaps);
adapt.notifyDataSetChanged();
which update the adapter of the grid.
I'm doing this 3 lines inside the onResume() method but after the thread finish i need to call the onResume() method somehow (by pausing the activity or somthing simillar).
now if i'm moving to another acitivity (like one of the grid images) and then press the back button i can see the grid view image that i just downloaded from the database. (because it calls onPause() method and then onResume() )
Doe's anyone have a solution to this problem?
Thanks
Edit:
The thread is running through AsyncTask
after the thread finish i need to call the onResume() method somehow (by pausing the activity or somthing simillar).
Rather than call onResume() by force, just move those three lines into a new method, call it refreshAdapter(). Then call refreshAdapter() inside onResume() and anywhere use you want to refresh the Adapter.

Categories