views:

35

answers:

2

What I did was create two .java files. One that can compile and run on a 1.5 phone (SDK3) and then one that works on 2.0(SDK5) So for this example i'll call the 1.5 file ExampleOld and the new one Example. I was wondering if i just made activity like this if it would work sort of like a "portal" and pick the activity to load depending on the SDK so there is no crash or compile errors. Are there any changes I should make to my code? Maybe anyone out there that's had to do this before. thanks!

package com.my.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

public class ExamplePortal extends Activity { 
    int sdk=new Integer(Build.VERSION.SDK).intValue();

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);

    if (sdk<5) {
        Intent v = new Intent(this, ExampleOld.class);
        startActivity(v);
    }

    else {
        Intent v = new Intent(this, Example.class);
        startActivity(v);
    } 
}

}
+2  A: 

What you're doing (correct me if I'm wrong) is trying to maintain backwards compatibility while making use of new APIs if the user is running a newer android version. The best way to do this is to follow the tutorial Google posted here. This avoids any verification issues and is really the best way to do stuff imho.

QRohlf
I was looking at that. I'm just still pretty new to java. I had a hard time following that. So, I tried to come up with something I understood.
brybam
A: 

I would put this decision in a Factory Class to avoid having these if-else statements all over the codebase.

Peter Tillemans