Hello friends, I have a question for you. I have an android app which basically are two activities the main_activity
and a details activity
. The first has a recyclerView
with elements and when I touch one I enter the activity details the issue is that when I enter the activity_detalles
the consumption of my app is doubled and goes from 65 mb to 125mb , when leaving this activity, this memory consumption is maintained, which means that users with a low-end telephone have failures.
How can I release memory when leaving the activity details? for this, here I put my activity details.
public class DetailActivity extends AppCompatActivity {
public static final String EXTRA_POSITION = "position";
RatingBar ratingBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// MobileAds.initialize(getApplicationContext(), getString(R.string.llavedeapp));
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Set Collapsing Toolbar layout to the screen
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
// Set title of Detail page
// collapsingToolbar.setTitle(getString(R.string.item_title));
ratingBar = (RatingBar) findViewById(R.id.ratingBarId);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(getApplicationContext(), getString(R.string.calificado) + " " + ratingBar.getRating() + " " + getString(R.string.estrellas), Toast.LENGTH_SHORT).show();
}
});
int postion = getIntent().getIntExtra(EXTRA_POSITION, 0);
Resources resources = getResources();
String[] places = resources.getStringArray(R.array.nombre_coctel);
collapsingToolbar.setTitle(places[postion % places.length]);
String[] placeDetails = resources.getStringArray(R.array.coctel_ingredientes);
TextView placeDetail = (TextView) findViewById(R.id.place_detail);
placeDetail.setText(placeDetails[postion % placeDetails.length]);
String[] placeLocations = resources.getStringArray(R.array.coctel_preparacion);
TextView placeLocation = (TextView) findViewById(R.id.place_location);
placeLocation.setText(placeLocations[postion % placeLocations.length]);
String[] acercaDelCoctel = resources.getStringArray(R.array.coctel_descripcion);
TextView acercaCoctel = (TextView) findViewById(R.id.acerca_del_coctel);
acercaCoctel.setText(acercaDelCoctel[postion % acercaDelCoctel.length]);
final TypedArray placePictures = resources.obtainTypedArray(R.array.places_picture);
ImageView placePicutre = (ImageView) findViewById(R.id.image);
placePicutre.setImageDrawable(placePictures.getDrawable(postion % placePictures.length()));
TextView textHistoriaCoctel = (TextView) findViewById(R.id.TextHistoria);
String[] historiaDelCoctel = resources.getStringArray(R.array.historia_coctel);
TextView historiaCoctel = (TextView) findViewById(R.id.historia_coctel);
//Verifico que el cóctel tenga una historia en el el array de historia
//de lo contrario desaparezco las vistas de la pantalla
if ((historiaDelCoctel[postion]).isEmpty()) {
textHistoriaCoctel.setVisibility(View.GONE);
historiaCoctel.setVisibility(View.GONE);
} else {
textHistoriaCoctel.setVisibility(View.VISIBLE);
historiaCoctel.setVisibility(View.VISIBLE);
historiaCoctel.setText(historiaDelCoctel[postion % historiaDelCoctel.length]);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
MobileAds.initialize(getApplicationContext(), "ca-app-pub-8600901870293215/4069468888");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
placePictures.recycle();
ImageButton favoriteImageButton = (ImageButton) findViewById(R.id.share_button);
favoriteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, conformarCuerpoEmail());
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
ImageButton shareImageButton = (ImageButton) findViewById(R.id.email_button);
shareImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
}
});
}
/* Se crea un hilo al llamar el método @sendEmail lo que hace que la app trabaje mejore el rendimiento en
* telefonos de baja gama.*/
public void sendEmail() {
new Thread(new Runnable() {
public void run() {
composeEmail("[email protected]", getString(R.string.comohacer), conformarCuerpoEmail());
}
}).start();
}
/*El método @conformarCuerpoEmail es el encargado de realizar el String correspondiente al cuerpo del mensaje
* en el correo*/
public String conformarCuerpoEmail() {
int posicion = getIntent().getIntExtra(EXTRA_POSITION, 0);
Resources resources = getResources();
//Accediendo al nombre del Cocktel
String[] places = resources.getStringArray(R.array.nombre_coctel);
String nombreTrago = places[posicion];
//Accediendo al arreglo de Descripciones
String[] place_desc = resources.getStringArray(R.array.coctel_descripcion);
String descripcionTrago = place_desc[posicion];
//Accediendo al arreglo de Ingredientes
String[] placeDetails = resources.getStringArray(R.array.coctel_ingredientes);
String ingredientesTrago = placeDetails[posicion];
String[] placeLocations = resources.getStringArray(R.array.coctel_preparacion);
String preparacionTrago = placeLocations[posicion];
String cuerpoEmail;
cuerpoEmail = "" + getString(R.string.nombre_trago) + "" + nombreTrago + "" + "\n"
+ getString(R.string.item_desc) + "\n" + " " + descripcionTrago + "\n"
+ " " + getString(R.string.ingredientes) + "\n" + " " + ingredientesTrago + "\n"
+ " " + getString(R.string.preparacion) + "\n" + " " + preparacionTrago;
//Se devuelve el Cuerpo del mensaje que será enviado por correo
return cuerpoEmail;
}
@Override
protected void onPostResume() {
super.onPostResume();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
/* El método @composeEmail es el que conforma el correo lanazando un Intent*/
public void composeEmail(String addresses, String subject, String body) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
// Uri.parse("file://"+ Environment.getDataDirectory());
startActivity(Intent.createChooser(intent, "Send Email"));
}
}