1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-31 20:11:58 +02:00

Merge branch 'context' of git://github.com/erijo/openScale into erijo-context

This commit is contained in:
OliE
2018-02-26 17:03:20 +01:00
6 changed files with 60 additions and 30 deletions

View File

@@ -94,7 +94,7 @@ public class OpenScale {
public static OpenScale getInstance(Context context) {
if (instance == null) {
instance = new OpenScale(context);
instance = new OpenScale(context.getApplicationContext());
}
return instance;
@@ -461,6 +461,10 @@ public class OpenScale {
fragment.updateOnView(scaleMeasurementList);
}
public void unregisterFragment(FragmentUpdateListener fragment) {
fragmentList.remove(fragment);
}
public void updateScaleData() {
int selectedUserId = getSelectedScaleUserId();

View File

@@ -37,6 +37,7 @@ import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.FileProvider;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
@@ -76,11 +77,10 @@ public class MainActivity extends AppCompatActivity
private static boolean firstAppStart = true;
private static boolean valueOfCountModified = false;
private static int bluetoothStatusIcon = R.drawable.ic_bluetooth_disabled;
private static MenuItem bluetoothStatus;
private MenuItem bluetoothStatus;
private static final int IMPORT_DATA_REQUEST = 100;
private Fragment currentFragment;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private NavigationView navDrawer;
@@ -110,8 +110,6 @@ public class MainActivity extends AppCompatActivity
setContentView(R.layout.activity_main);
currentFragment = null;
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@@ -288,28 +286,23 @@ public class MainActivity extends AppCompatActivity
Class fragmentClass;
String fragmentTitle;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
switch(menuItemId) {
switch (menuItemId) {
default:
case R.id.nav_overview:
fragmentClass = OverviewFragment.class;
fragmentTitle = getResources().getString(R.string.title_overview);
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
break;
case R.id.nav_graph:
fragmentClass = GraphFragment.class;
fragmentTitle = getResources().getString(R.string.title_graph);
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
break;
case R.id.nav_table:
fragmentClass = TableFragment.class;
fragmentTitle = getResources().getString(R.string.title_table);
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
break;
case R.id.nav_statistic:
fragmentClass = StatisticsFragment.class;
fragmentTitle = getResources().getString(R.string.title_statistics);
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
break;
case R.id.nav_settings:
Intent settingsIntent = new Intent(this, SettingsActivity.class);
@@ -322,35 +315,45 @@ public class MainActivity extends AppCompatActivity
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/oliexdev/openScale/wiki")));
drawerLayout.closeDrawers();
return;
default:
fragmentClass = OverviewFragment.class;
fragmentTitle = getResources().getString(R.string.title_overview);
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putInt("lastFragmentId", menuItemId).commit();
FragmentManager fragmentManager = getSupportFragmentManager();
// hide previous fragment if it available
if (currentFragment != null) {
fragmentManager.beginTransaction().hide(currentFragment).commit();
// Make sure that any pending transaction completes so that added fragments are
// actually added and won't get added again (may happen during activity creation
// when this method is called twice).
fragmentManager.executePendingTransactions();
FragmentTransaction transaction = fragmentManager.beginTransaction();
final String tag = String.valueOf(menuItemId);
boolean found = false;
for (Fragment fragment : fragmentManager.getFragments()) {
if (fragment.getTag().equals(tag)) {
// Show selected fragment if already added
transaction.show(fragment);
found = true;
}
else if (!fragment.isHidden()) {
// Hide currently shown fragment
transaction.hide(fragment);
}
}
// try to find selected fragment
currentFragment = fragmentManager.findFragmentByTag(""+menuItemId);
// if fragment not found then add the fragment
if (currentFragment == null) {
// If fragment isn't found then add it
if (!found) {
try {
currentFragment = (Fragment) fragmentClass.newInstance();
transaction.add(R.id.fragment_content, (Fragment) fragmentClass.newInstance(), tag);
} catch (Exception e) {
e.printStackTrace();
}
fragmentManager.beginTransaction().add(R.id.fragment_content, currentFragment, "" + menuItemId).commit();
} else { // otherwise show fragment
fragmentManager.beginTransaction().show(currentFragment).commit();
}
transaction.commit();
// Set action bar title
setTitle(fragmentTitle);
@@ -654,7 +657,6 @@ public class MainActivity extends AppCompatActivity
R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
}
currentFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

View File

@@ -226,6 +226,12 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
return graphView;
}
@Override
public void onDestroyView() {
OpenScale.getInstance(getContext()).unregisterFragment(this);
super.onDestroyView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

View File

@@ -148,6 +148,12 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
return overviewView;
}
@Override
public void onDestroyView() {
OpenScale.getInstance(getContext()).unregisterFragment(this);
super.onDestroyView();
}
@Override
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
if (scaleMeasurementList.isEmpty()) {

View File

@@ -156,6 +156,12 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
return statisticsView;
}
@Override
public void onDestroyView() {
OpenScale.getInstance(getContext()).unregisterFragment(this);
super.onDestroyView();
}
@Override
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();

View File

@@ -97,6 +97,12 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
return tableView;
}
@Override
public void onDestroyView() {
OpenScale.getInstance(getContext()).unregisterFragment(this);
super.onDestroyView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);