1

So I have this fab menu and each button have different activity what I want is to put the functions into one java file instead of creating a lot of java file to call different functions in the button

Here's the one of the java file - SVAuditorium.class.

public class SVAuditorium extends AppCompatActivity {

    private static final LatLng Auditorium = new LatLng(10.294335, 123.880809);

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.street_view);

        SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
                (SupportStreetViewPanoramaFragment)
                        getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama);
        streetViewPanoramaFragment.getStreetViewPanoramaAsync(
                new OnStreetViewPanoramaReadyCallback() {
                    @Override
                    public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
                        if (savedInstanceState == null) {
                            panorama.setPosition(Auditorium);
                        }
                    }
                });
    }

}

This is what I did to call the activities from the SVAuditorium and the rest.

case R.id.fab_auditorium:
    startActivity(new Intent(this, SVSAuditorium.class));
    break;
case R.id.fab_library:
    startActivity(new Intent(this, SVLibrary.class));
    break;
case R.id.fab_main:
    startActivity(new Intent(this, SVMainBuilding.class));
    break;
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Batmon
  • 11
  • 2
  • which functions you want in one java file? – Zaid Mirza Feb 22 '18 at 04:46
  • SVAuditorium because I have like 10 SVnamed java how to put all of this like in one java file @ZaidMirza – Batmon Feb 22 '18 at 04:47
  • 2
    your SVNamed classes representing Activities,not functions. – Zaid Mirza Feb 22 '18 at 04:49
  • `private static final LatLng Auditorium = new LatLng(10.294335, 123.880809);` I only need to change the latitute and longitude in each java file so is it ok to create this many java?? @ZaidMirza – Batmon Feb 22 '18 at 04:50
  • I think this is Ok . Its all depends on what what exactly the function does . For instance a function `dpToPX()` convert the value to pixel and uses throughout the application so in this case its better to put this function in a Utility class as `dpToPx(Context context)`. Get the point ? – ADM Feb 22 '18 at 04:50
  • 1
    Elaborate pls @ADM – Batmon Feb 22 '18 at 04:51
  • @Batmon are you saying all SVnamed files have same functionality but only LatLng is different? – Zaid Mirza Feb 22 '18 at 04:57
  • Yes the Latlng is only different @ZaidMirza – Batmon Feb 22 '18 at 04:58
  • I guess you have created one class per StreetView – Zaid Mirza Feb 22 '18 at 05:02

2 Answers2

1

As you have stated that you only have changes in lat-long in each of your Activity, I would suggest keeping a single activity like you want and pass the lat-long through intent. Here's a sample code showing how you can achieve the behaviour.

Intent intent = new Intent(MainActivity.this, SVActivity.class);
Bundle longLat = new Bundle();
longLat.putDouble("LONGITUDE", longitude);
longLat.putDouble("LATITUDE", latitude);
intent.putExtras(longLat);
startActivity(intent);

Get the latitude and longitude in your SVActivity like this.

Bundle longLat = getIntent().getExtras();
double longitude = longLat.getDouble("LONGITUDE");
double latitude = longLat.getDouble("LATITUDE");

Initialize your views based on the value found in your SVActivity.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
1

the Latlng is only different

Since LatLng implements Parcelable

You need only need one Java file and you can pass the LatLng through an Intent using Intent.putExtra("latlng", latlng), where you define that variable per clicked item

See answer - Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

Then in that one Activity's onCreate method, you can LatLng latlng = getIntent().getParcelableExtra("latlng")

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245