
- Forum posts: 1
Apr 21, 2017, 8:45:52 PM via Website
Apr 21, 2017 8:45:52 PM via Website
Hey Everyone
I'm new here, i hope you can help me with my problems..
I developed an new App for something like a school, but i have a few problems with my app on older devices. Sometimes there crash with outOfMemory (Heap) Exception.. i already removed some large images, but the App still use a lot of the Heap Memory.
They also use ~85 mb of RAM, just to display Lot of Constrainet Layout, Text, Buttons & ImageViews (Just with Icons in it) and round about 10 Intents (Of course not at the same time )
Is there a way to reduce the amount of RAM/Heap Memory the App use?
Maybe someone can give me some tips how to code a bit more memory efficiency
I'll be glade if someone can help me !
Devices:
- Galaxy S7 (Android Version: current one, No problems at all)
- Galaxy S4 (Android version: 4.4, crashes)
Here is a little bit of my Code :< it's my first bigger project, so i think i make a lot of mistakes :'D
MainActivity:
public class ActivityMain extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public static PackageInfo packageInfo;
private int infoCounter;
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
// Package Infos Laden
try {
packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
} catch (Exception ignored) {
}
///////////////////////////////////////
// Speichern / Laden vorbereiten
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
///////////////////////////////////////
// Menü Laden & Funktionen zuweisen
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openEmail();
}
});
///////////////////////////////////////
// Informationstext anzeigen
showInformation();
///////////////////////////////////////
// Prüfen ob es sich um eine gekaufte Demo Version handelt
if (!sharedPreferences.getString("key", "").isEmpty()) {
if (Config.checkKey(sharedPreferences.getString("key", ""))) {
Toast.makeText(getApplicationContext(), "Verifizierung erfolgreich!", Toast.LENGTH_SHORT).show();
Config.isBuyed = true;
}
else {
Toast.makeText(getApplicationContext(), "Verifizierung fehlgeschlagen!", Toast.LENGTH_SHORT).show();
Config.isBuyed = false;
}
} else {
Config.isBuyed = false;
}
///////////////////////////////////////
// Werbung entsprechend laden
if ((Constants.type == Constants.Type.FULL && Config.adFullversion) ||
(Constants.type == Constants.Type.DEMO && !Config.isBuyed)) {
AppBarLayout appBar = (AppBarLayout)findViewById(R.id.appbar);
AdView mAdView = new AdView(this);
mAdView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//mAdView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(getString(R.string.ad_banner_main));
appBar.addView(mAdView);
MobileAds.initialize(getApplicationContext(), "[PLACEHOLDER]");
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (Config.isDebug) {
adRequestBuilder.addTestDevice(getString(R.string.ad_test_device));
}
AdRequest adRequest = adRequestBuilder.build();
mAdView.loadAd(adRequest);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_info) {
showInformation(true);
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
return FragmentMain.newInstance(position + 1);
case 1:
return FragmentInfo.newInstance(position + 1);
case 2:
return FragmentAbout.newInstance(position + 1);
default:
return FragmentMain.newInstance(1);
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.tab01);
case 1:
return getString(R.string.tab02);
case 2:
return getString(R.string.tab03);
}
return null;
}
}
//////////////////////////////////////////
// Extern Functions
public void switchActivity(View view) {
Intent intent = null;
switch (view.getId()) {
case R.id.image_menu_bfw:
intent = new Intent(this, ActivityBFW.class);
break;
default:
Log.d("ERROR", "CANT START INTENT!");
break;
}
if (intent != null)
startActivity(intent);
intent = null;
// TODO: Passende Animation finden & Rückweg fixen (OnPressBack Button in Intent?)
// overridePendingTransition(R.anim.bottom_to_top, R.anim.stay);
}
private void showInformation() {
showInformation(false);
}
private void showInformation(boolean ignoreCounter) {
infoCounter = sharedPreferences.getInt("infoCounter", 3);
if ((infoCounter > 0) || ignoreCounter) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Information");
alertDialog.setMessage(getResources().getString(R.string.label_error_information) + (!ignoreCounter ? "\n\nVerbleibende Einblendungen: " + (infoCounter - 1) : ""));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editor.putInt("infoCounter", --infoCounter);
editor.commit();
}
});
alertDialog.show();
}
}
private void openEmail() {
Intent intent;
intent = new Intent(this, ActivityEmail.class);
startActivity(intent);
}
public void exitApplication() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
public void openApp(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=de.*.*.*"));
startActivity(intent);
}
//////////////////////////////////////////
// Dialogs
public void showDialog(String title, String message, String buttonText) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, buttonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
public void showCloseDialog(String title, String message, String buttonText) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, buttonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
alertDialog.show();
}
public void register(View view) {
Intent intent = new Intent(this, ActivityRegister.class);
startActivity(intent);
}
}
An other important Activity:
public class ActivityBFW extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bfw);
}
public void switchActivity(View view) {
Intent intent = null;
switch (view.getId()) {
case R.id.image_menu_eating:
intent = new Intent(this, ActivityEating.class);
break;
case R.id.image_menu_gym:
intent = new Intent(this, ActivityGYM.class);
break;
case R.id.image_menu_fitness:
intent = new Intent(this, ActivityFitness.class);
break;
case R.id.image_menu_pool:
intent = new Intent(this, ActivityPool.class);
break;
case R.id.image_menu_shop:
intent = new Intent(this, ActivityShop.class);
break;
default:
Log.d("ERROR", "CANT START INTENT!");
break;
}
if (intent != null)
startActivity(intent);
intent = null;
// TODO: Passende Animation finden & Rückweg fixen (OnPressBack Button in Intent?)
// overridePendingTransition(R.anim.bottom_to_top, R.anim.stay);
}
}
— modified on Apr 21, 2017, 8:49:58 PM