How to call an activity's onResume method from a Receiver - android

I have an activity named GameTray where drawings carried are out. I have a ScreenReceiver which extends BroadcastReceiver, this is for detecting screen lock and phone lock. I want to save the datas while a screen lock occurs in the GameTray activity. onResume method of GameTray contains all the saving things. So I have a doubt, How can I call the onResume method method of GameTray activity in the ScreenReceiver class while overriding onReceive method? Am attaching the source for ScreenReciever here. Thanks in advance
public class ScreenReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
// Log.v("$$$$$$", "In Method: ACTION_SCREEN_OFF");
// onPause() will be called.
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
// Log.v("$$$$$$", "In Method: ACTION_SCREEN_ON");
// HERE I WANT TO CALL THE ONRESUME METHOD OF THE ACTIVITY GAMETRAY.
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
// HERE I WANT TO CALL THE ONRESUME METHOD OF THE ACTIVITY GAMETRAY.
}
}
}

onResume method of GameTray contains all the saving things.
onResume() is a method that is part of an Activity life cycle, and it is called when the Activity comes to foreground, if you want to call it, you should start/resume the activity.
But why not create a separate class, or method, that will do all the saving things and be called in onResume() as well as in onReceive().

Related

Removing Preference data when Application killed

In my application i am storing some SharedPreference data.
I have to clear that all stored data when application killed.
So, I have done it in my activity's onDestroy() as below :
#Override
protected void onDestroy() {
if(isBackPressed==0){
if(Prefrences.checkPref(MyActivity.this,MAIN_PREF)){
Prefrences.removePref(MyActivity.this,MAIN_PREF);
Prefrences.removePref(MyActivity.this,PREF_1);
Prefrences.removePref(MyActivity.this,PREF_2);
Constant.displayLogE(">>>>>>>>>>","### Prefrence removed ");
}
Constant.displayLogE(">>>>>>>>>>","### Destroy activity ");
}
finish();
super.onDestroy();
}
Here, I have taken isBackPressed because, When onBackPressed called it calles finish() automatically and onDestroy() method calls. So, I have initialized isBackPressed to 1 inside onBackPressed() method.
It doesn't matter, I just have to remove my prefrence data when app going to be killed. But, the issue is when I killing app, onDestroy() methos not calling.
Thanks.
try this way.
public class App extends Application{
#Override
public void onCreate() {
doSomeCleanWork();
}
}

Access TextViews from broadcast receiver

In my broadcast receiver, I'm accessing a method that calls another method which is in my MainActivity class. The method from my MainActivity class uses variables that are set in the onCreate() method.
The problem I'm having is that when the broadcast receiver tries to access this method when the app has been closed, it finds the variables null because the onCreate method hasn't ran to set the variables.
Should I try to trigger the onCreate method from my broadcast receiver, or do I have this whole setup wrong? I tried to condense the code a bit so it's not too long but below you can see the gist of what I'm trying to do.
The error I'm getting is that "text" and "ringer" are null when trying to setText. This only happens when the broadcast receiver runs while the app is closed and not running.
WifiScanCompleteReceiver code:
public class WifiScanCompleteReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
...
}
public static void activate() {
...
MainActivity.statusText();
}
}
Activity code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
...
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.state);
ringer = (TextView)findViewById(R.id.mode);
state = mSettings.getString("state", "init");
mode = mSettings.getInt("ringer", 0);
statusText();
}
public static void statusText() {
text.setText(state);
if (mode == 1) {
ringer.setText("Vibrate");
} else if (mode == 2) {
ringer.setText("Normal");
} else {
ringer.setText("Unkown");
}
}
}
Instead of accessing the TextView from the receiver, I just triggered the main activity as a service with context.startService(Intent); and that seemed to get me what I wanted.
I needed to trigger the code in my main activity to run even if the main activity had been stopped. So this solved that for me.
You can solve this using interface:
1) Create an interface
interface MyListener {
public void doSomething();
}
2) Initialize the Listener in BroadcastReceiver
public class WifiScanCompleteReceiver extends BroadcastReceiver {
private MyListener listener;
public void onReceive(Context context, Intent intent) {
listener = (MyListener)context;
listener.doSomething(); // Call interface method
}
}
3) Implement interface in the Activity and override the method
public class MainActivity extends Activity {
// Your Activity code
public static void statusText() {
text.setText(state);
if (mode == 1) {
ringer.setText("Vibrate");
} else if (mode == 2) {
ringer.setText("Normal");
} else {
ringer.setText("Unkown");
}
}
#Override
public void doSomething(){
statusText();
}
}
Relevant Link:
If you want to read the advantage of using interface Read this
Its not a good approach to call activity's method directly from the receiver. App will crash in a case when your activity is not visible, but due to receiver's call it will try to execute activity's code.
You can use local broadcast here. Instead of calling activity's method from receiver send local broadcast, which you need to register in your activity and in receiver of local broadcast call your activity method. This method(Activity's method) call from local broadcast will only execute when your activity will be visible and will not result in app crash.

Can we destroy activity without call stop() method?

In activity life cycle to execute as like.
onCreate()
onDestroy()
it means without call stop() method in activity life cycle.
how it is possible ?
Use finish(); to destroy activity.
You don't need to call stop() method. Android system automatically go thru those life cycle methods.
But apparently onDestroy() always called after onStop().
If you want to kill activity just call finish(), it will destroy your activity.
But remember again onStop() always called as system level, follows the activity life cycle if you call finish().
Note: If system kills your application or activity to utilize memory there is no guarantee to call these methods from activity life cycle.
public class ExampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty);
System.out.println("in onCreate");
finish();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("in onDestroy");
}
}
when the activity run then call onCreate() method and onDestroy()
method

Android activity state

In Android applications, is it possible to be notified when the activity is paused
but without using onDestroy method in the activity class.
In other words, I want the operations to be outside the main class.
You can use onPause() method of Activity life cycle:
just override onPause method.
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
When activity is going to pause. it will call this method.

Finish subclass activity from its superclass

Consider the following scenario:
The class TemplateActivity extends Activity. Within onResume() it performs a validation of a boolean variable then, if false, it finishes the method and the activity, and starts a new activity, OtherActivity.
When the class ChildActivity, which extends TemplateActivity, runs, it waits for super.onResume() to finish and then continue no matter if its super needs to start the Intent or not.
The question:
Is there a way to terminate the ChildActivity when the OtherActivity needs to start from the TemplateActivity? Without implementing the validity check in the child class.
Superclass:
class TemplateActivity extends Activity {
#Override
protected void onResume() {
super.onResume();
if(!initialized)
{
startActivity(new Intent(this, OtherActivity.class));
finish();
return;
}
//Do stuff
}
}
Subclass:
class ChildActivity extends TemplateActivity {
#Override
protected void onResume() {
super.onResume();
//Do stuff
}
}
A more elegant solution would be a slightly different approach to the class design:
Introduce a method DoStuff() (replace with sensible name) in the TemplateActivity . Do all the // do stuff bits there.
Call this method from the end of TemplateActivity OnResume
Override it in the child activity to extend it with the child activity // do stuff bits.
Do not override onResume in the ChildActivity.
This way, if the condition fires in TemplateActivity OnResume, none of the parent and child DoStuff will be done. The ChildActivityshouldn't have to know anything about this behavior.
I guess this is what you're trying to get:
class ChildActivity extends TemplateActivity {
#Override
protected void onResume() {
super.onResume();
if (!isFinishing()) {
// Do stuff
}
}
}

Categories

Resources