Overriding activity transitions for every activity - android

I've created some animations for activity transition. But the code for overriding each activity's transition is kind of boilerplate code, how can I overcome this, meaning writing the code once and being applied to every activity?I'm guessing something with inheritage ?

class BaseActivity extends Activity {
public void transition {
overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
}
}
class SubActivitiy extends BaseActivity {
public void applyTransition {
transition();
}
}

Related

How to get reference of currently visible activity without passing it as parameter to other class

As the title says, I want to get the reference of currently visible activity or you can say activity which is at the top of backstack from a class, I don't want to send activity reference to that class, because I am using that class from many activities and if I do, I have to pass activity reference from every activity which is a long process.
I already have seen many answers which are typecasting context reference to activity but it is not working.
If anyone has the idea of how to do that in a short way, then please share.
I don't know what you mean by "long process".
Normally, if the Activity is delegating work to another class, it needs to pass itself as a reference so that the delegate knows how to call back the Activity to report progress, etc. This is standard Android stuff. AsyncTask works like this (as an example).
However, if all you want to do is display a Dialog, then you can, instead, start an Activity that looks like a Dialog. This is also a pretty common Android solution. There are themes that you can apply to an Activity that make it look just like a Dialog.
I have found a way via we can do this, In your application class add: registerActivityLifecycleCallbacks(this will listen to all activity lifecycle methods), like this:
public class MyApplication extends Application {
public static Activity currentActivity=null;
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(#NonNull Activity activity, #Nullable Bundle bundle) {
}
#Override
public void onActivityStarted(#NonNull Activity activity) {
currentActivity=activity;
}
#Override
public void onActivityResumed(#NonNull Activity activity) {
}
#Override
public void onActivityPaused(#NonNull Activity activity) {
}
#Override
public void onActivityStopped(#NonNull Activity activity) {
}
#Override
public void onActivitySaveInstanceState(#NonNull Activity activity, #NonNull Bundle bundle) {
}
#Override
public void onActivityDestroyed(#NonNull Activity activity) {
}
});
}
}
And then use it like:
if (MyApplication.currentActivity!=null){
// your code here
}
Don't forget to add your application class to manifest:
<application
android:name=".MyApplication"
.../>

Get & Set Value of my Activity from a fragment - Android

I would like know how can I get and set a value of my activity from my fragment?? is that possible?
below is my activity and the attribute 'myStation' is the value I want get and set from my fragment.
public class MyActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
public static Station myStation;
In my fragment I can execute 'getActivity()' but I really don't know if I can do that. if I'm wrong, what is the correct process?¿
Thanks.
If the fragment is only used in that activity, then you can simply cast the activity. Otherwise you'll have to verify that it is the correct activity perhaps using instance of.
Let's look at the simpler case:
public class MyActivity extends Activity {
private boolean myFlag;
public boolean getMyFlag() {
return myFlag;
}
public void setMyFlag(boolean myFlag) {
this.myFlag = myFlag;
}
And here would be the fragment to adjust the flag.
public class MyUniqueFragment extends Fragment {
public void updateActivityFlag(boolean myFlag) {
MyActivity myActivity = (MyActivity) getActivity();
myActivity.setMyFlag(myFlag);
}
}

Android - execute code onResume and onPause for all the activities of the app?

I'm designing an architecture where the app needs to execute certain set of operations everytime it goes to background (onPause) and a set of operations everytime it comes back to foreground (onResume), irrespective of the activity (for all the activities). Is there a way with which I can achieve this, without having to call those methods in every activity class' onPause and onResume overrides?
Make your own class that extends Activity and add your desired behavior to its onPause and onResume methods.
Then you extend that class on your activities.
public class BaseActivity extends Activity {
#Override
protected void onPause() {
// ...
}
#Override
protected void onResume() {
// ...
}
}
public class Activity1 extends BaseActivity {
// ...
}
You could extends your Activities by a BaseActivity which extends Activity, and create the two methods onPause / onResume in it.
See this answer for more information.

Write a method just once and access it in "android:onClick" everywhere in the application

I have a title bar in my application. And the title bar has one button. On click of that button I display info activity. Now, as far as I know, android:onClick needs a reference of a public method inside the activity which has the xml set in setContentView(). Now, as the logic for that buttons click will be the same throughout the application, so what I want is, I will the method just once say showInfoScreen(View view) and put it in that buttons onClick attribute. And I need not write the same method everywhere. Is it possible?
Of course it's possible. Write an Activity class, then have all of your Activitys extend it. For example:
public abstract class BaseActivity extends Activity {
#Override
public void setContentView( int layoutResID ) {
super.setContentView( layoutResID );
findViewById(R.id.button).setOnClickListener(new OnTitleBarButtonClickListener());
}
private void showInfoScreen() {
// Show the info screen
}
private class OnTitleBarButtonClickListener implements OnClickListener {
#Override
public void onClick(View v) {
showInfoScreen();
}
}
}
Then all of your derived Activitys would extend BaseActivity instead of Activity.
The beauty of doing it this way is that any Activity that extends this class automatically gets this feature. No coding is required in the derived classes, just in BaseActivity. The only contract all of your Activitys will have will be to have R.id.button or whatever id you name it within its content.
I think you have to write onclick in every Activity where you want to display infoscreen.
But OnClick you just call A method showInfoScreen(View view) in every Activity....
And you should create class Like...ShowInfo and there are one static method...
public class ShowInfo{
public static void showInfoScreen(View view,Context c){
//now dispay info here
}
}
Write ShowInfo.showInfoScreen(v,YourClassName.this) in your onClick() Method....
An example of what Vinayak.B suggested is like this:
public class yourAppUtils {
public static void yourMethod() {
// Do stuff
}
}

roboguice replace Activity with own implementation

Is there a way to replace the default Activity class with own implementation extending from this class using roboguice?
For instance an activity like this:
public class MyActivity extends Activity
{...}
would replace the default Activity class and would become the base activity for all other derived activities.
Yes. The easiest way is to have your base activity extend from RoboActivity.
Eg.
class MyBaseActivity extends RoboActivity { ... }
And then have all of your activities extend from MyBaseActivity.
However, if for some reason you don't wish to extend from RoboActivity, you can easily add injection to your own activities by doing the following:
class MyBaseActivity extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
RoboGuice.getInjector(this).injectMembersWithoutViews(this);
}
public void onContentChanged() {
super.onContentChanged();
RoboGuice.getInjector(this).injectViewMembers(this);
}
}
Take a look at RoboActivity's source for more details. As long as you don't need events, the changes required are quite simple.

Categories

Resources