Overriding method in secondary class - android

I have a super-class called MainActivity and a secondary class called Insert.
The two class are defined as the code below.The Insert class extends the MainActivity class.
In the two class I have the onStop() method, and in the Insert class I have #Override. The problem is that when onStop is executed in the Insert class, also the onStop method of MainActivity is called. Why? How can I do?
Thank you!
public class MainActivity extends AppCompatActivity {
public void onStop() {
//some code
super.onStop();
}
}
public class Insert extends MainActivity {
#Override
public void onStop() {
//some code
super.onStop();
}
}

onStop is the default method provided by activity lifecycle. You can have your custom method name say onMyStop() and call that method from Insert class's onStop using super.onMyStop()

The reason is calling super.onStop() in Insert overriden method. But calling super.onStop is mandatory. To fix it, you can create another method like doOnStop in MainActivity, call it inside onStop of MainActivity and override in Insert without calling super.doOnStop.
Here is code sampe:
public class MainActivity extends AppCompatActivity {
public void onStop() {
super.onStop();
doOnStop();
//some code
}
public void doOnStop() {
}
}
public class Insert extends MainActivity {
#Override
public void doOnStop() {
//some code
}
}

Related

Notify when onPause() called in any activity of an application

I think this question may simple but I didn't find any solution for this,
I there any way in Android that if any one of an activity calls onPause() I need to show Toast message or any notification kind of thing need to show. Generally I want to get notified when activity calls onPause() but I need it in one place since I may have some 15 activity I don't want to add it in all the activity.
ex:If I have activity when any one of the activity calls onPause I need to get notified but that notification code should be in one place and we should not add any line of code onPause() Is it possible to do this.
Thanks.
Create a baseActivity, which has for example :
open class BaseActivity : AppCompatActivity() {
override fun onPause() {
super.onPause()
Toast.makeText(this, "notified", Toast.LENGTH_SHORT).show()
}
}
Then you can extends this in your activities and handle the on pause call in BaseActivity
If your minSdkVersion >= 14, you can use Application.ActivityLifecycleCallbacks: ActivityLifecycleCallbacks
You have to define a custom Application class and you can register for this callbacks afterwards:
public class MyApplication extends Application {
private class LifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
#Override
public void onActivityCreated(final Activity activity, final Bundle savedInstanceState) {
//nothing to do
}
#Override
public void onActivityDestroyed(final Activity activity) {
//nothing to do
}
#Override
public void onActivityPaused(final Activity activity) {
// TODO Do your stuff, e.g. show toast.
}
#Override
public void onActivityResumed(final Activity activity) {
//nothing to do
}
#Override
public void onActivitySaveInstanceState(final Activity activity, final Bundle outState) {
//nothing to do
}
#Override
public void onActivityStarted(final Activity activity) {
}
#Override
public void onActivityStopped(final Activity activity) {
}
}
private final LifecycleCallbacks callbacks;
public void onCreate() {
super.onCreate();
callbacks = new LifecycleCallbacks();
application.registerActivityLifecycleCallbacks(callbacks);
}
}
Create a BaseActivity which contain all the methods you want to use in all other activities.
Then extend every activity with BaseActivity to call onPause() method.

onResume for android annotations

I am using android annotations and have some code that I need to execute in the onResume() function in my activity.
Is it safe to just override the onResume function from the android annotation activity (ie with #EActivity)?
Yeah, you should use these lifecycle methods just like with plain Android activities. There is one thing though: injected Views are not yet available in your onCreate method, this is why #AfterViews exist:
#EActivity(R.layout.views_injected)
public class ViewsInjectedActivity extends Activity {
#ViewById
Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// myButton is not yet available here
}
#AfterViews
void setupViews() {
// myButton is first available here
myButton.setText("Hello");
}
#Override
protected void onResume() {
super.onResume();
// just as usual
}
}
Yeah. Just call super.onResume() and then add your code.
I'd do it just like their on create example here: https://github.com/excilys/androidannotations/wiki/Enhance-activities
You can bind your custom class with lifecycle component of android. It holds life cycle information of android component so that your custom class observe lifecycle changes.
public class MyObserver implements LifecycleObserver {
#OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void connectListener() {
...
}
#OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void disconnectListener() {
...
}
}
myLifecycleOwner.getLifecycle().addObserver(new MyObserver());

Call onResume() method from Asynctask's onPostExecute() Method

I am calling Asynctask and after complition of doInBackground(String... arg0) i want to call onResume() in onPostExecute() Method.
You should not call explicitly activity lifecycle methods, they usually call base class versions - ie. super.onResume(), so you might mess with activity state. Instead move related code from onResume to some outer function, and call this function instead in your onPostExecute.
Afte AsyncTask Complete
put this line in onPostExecute()
notifyDataSetChanged();
This Will call onResume() Automaticly
or if this is not Work then call Dialog box.
when Dialog box open Activity gosein onPause()
and when DialogBox is close it will call onResume()
this will work
Simply call Activity.this.onResume(); on your postexecution method
1.First of you need to take reference of your Activity say MainActivity in your asynkTask class by doing this.
MainActivity activity=(MainActivity)context.
context is the variable that you pass during calling the asynktask class from your activity.
2.now you can call easily by doing this.
activity.onResume().
Why do you need to do that?
if your async task is a nested class just call a method directly.
public MainActivity extends Activity{
//all the usual functionalities
public void methodAfterAsyncTask(){
//do stuff here
}
private CustomAsyncTask extends AsyncTask<Void,Void,Void>{
#Override
public onPostExecute(){
super.onPostExecute();
methodAfterAsyncTask();
}
}
}
If you are a strong believer of OOP and like clean code :)..I use this method
MainActivity .java
public MainActivity extends Activity{
//all the usual functionalities
public void onResume(){
super.onResume();
new CustomAsyncTask(new AsyncListener(){
public void postTaskMethod(){
//do stuff here
}
}).execute();
}
}
AsyncListener.java
public interface AsyncListener{
void postTaskMethod();
}
CustomAsyncTask.java
publicCustomAsyncTask extends AsyncTask<Void,Void,Void>{
private AsyncListener listener;
public CustomAsyncTask(AsyncListener listener){
this.listener=listener;
}
#Override
public onPostExecute(){
super.onPostExecute();
if(null!=listener)
listener.postTaskMethod();
}
}

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
}
}
}

How to call a method from one activity which is defined in another activity

Here is my Activity class
public class A extends Activity
{
.......
.......
//here i have to call my test() method of B activity
new B.test();
}
This is my next Activity
public class B extends Activity
{
......
public void test(){
//some code }
}
But the test() method is not not executing. I put a Toast message in test() method. but it is not showing the Toast message. How to call that test() method. Where am I wrong? Do i have to override onCreate() in B activity? Please tell me. Thanks in advance.
You can write test method in separate class which doesn't extends Activity.So that you can call the method from two activities whenever you need.
An activity is a different screen and you can call it by intent,so that it can be shown on screen
Do this.
public class A extends Activity{
B.test();
}
and
public class B extends Activity{
public static void test(){
System.out.println("test method of B is called.");
}
}

Categories

Resources