javaandroidandroid-activityandroid-transitions

Is there a way to create a class to share a transition intent? [building my first android app]


What I am trying to achieve here is create a java class that will allow me to call the method inside the class for an activity transitioning.

For instance, I have 3 activities: Main.xml, A activity.xml, B activity.xml. I want to create a class that I can call from main's java class to transition to A and then from A to B.

I thought maybe it would be somewhere along that lines of:

[Transitioning activity class] //Behind the scenes for activity transitioning, I don't know how to write this part or if calling it is correct

Intent intent = new Intent(?, ?); startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

[Main] //calling intent from activity transitioning class intent(this, A_activity.class);

[A Activity] //calling intent from activity transitioning class intent(this, B_activity.class);

Also I want to apologize, this is indeed my first rodeo, I am self-taught and have no professional experience in programming or developing. Thank you in advance!

So far what I am currently doing is creating the transitions in each class:

[A activity]

public void onClick(View v){
    switch(v.getId()){
        case R.id.commBtn:
            inqList.clear();
            inqAdd.addItem("Commercial");
            System.out.println(inqList);
            onTransition();
            break;
        case R.id.resBtn:
            inqList.clear();
            inqAdd.addItem("Residential");
            System.out.println(inqList);
            onTransition();
            break;
        default:
            break;
    }
}

//Method for transition after clicked
public void onTransition(){
    Intent intent = new Intent(this, b_scene.class);
    startActivity(intent);
    overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
**[Edit]**
///I did this and it works? not sure if its best practice but it worked.

[Transition Class]

    public class Transition {
        public static void onTrans(Activity activity, Class name) {
            Intent intent = new Intent(activity, name);
            activity.startActivity(intent);
            activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
        }
    }
[A Activity]
///Just put this in A activity

public void onClick(View v){
...
onTrans(this, B_Activity.class)
}

Solution

  • Sure you can but since that transition class is not an Activity instance (which has the startActivity() method), you need to pass the Activity instance as well, e.g.:

    public static void navigateTo(Activity source, Activity destination) {
        Intent intent = new Intent(activity, destination.class);
        source.startActivity(intent);
        source.overridePendingTransition(R.anim.fade_in,R.anim.fade_out); 
    }
    

    Another common option is to use an abstract BaseActivity that has this method onTransition() and extend your activities from this one (you can also use composition instead of inheritance for a better practice).