Reputation: 4863
I have FirstscreenActivity, in which I use RecyclerView, (in this RecyclerView displays pictures of the projects which were created by a user), then user goes to the next Activity, where he creates new project and adds pictures there. When user comes back to the FirstScreenActivity, old project downloads one more time, and after that download new project. How to make that old projects wouldn't download one more time after the changing the Activities (so there wouldn't be duplicates)?
public class FirstscreenActivity extends AppCompatActivity implements RecyclerItemClickListener.OnItemClickListener {
private MyAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
public static String mCurrentProject = null;
RecyclerView list;
static File[] listFile;
static File[] listFolders;
static int newpressed = 0;
public static ArrayList<Folder> FOLDERS = new ArrayList<>();
public static LruCache<String, Bitmap> mMemoryCache;
public static File[] listFile2;
public void getFromSdcardFolders() {
File file = new File(Environment.getExternalStorageDirectory() +
"/Audio_Recorder_Picture", "Previews");
if (file.isDirectory()) {
listFolders = file.listFiles();
for (int i = 0; i < listFolders.length; i++) {
Folder folderobject = new Folder();
folderobject.setName(listFolders[i].getName());
Log.i("List of FOLDERS: ", String.valueOf(listFolders[i].getName()));
File picturelist = new File(Environment.getExternalStorageDirectory() +
"/Audio_Recorder_Picture/Previews", listFolders[i].getName());
if (picturelist.isDirectory()) {
listFile = picturelist.listFiles();
for (int j = 0; j < listFile.length; j++) {
folderobject.addFile(listFile[j].getAbsolutePath());
}
}
FOLDERS.add(folderobject);
Log.wtf("TAG", "Folders size inside the getFRom:" + FOLDERS.size());
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("");
list = (RecyclerView) findViewById(R.id.list);
list.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
list.addOnItemTouchListener(new RecyclerItemClickListener(this, this));
getFromSdcardFolders();
list.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(this, FOLDERS);
list.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 4;
mMemoryCache
= new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addItem:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
Date now = new Date();
mCurrentProject = String.valueOf(formatter.format(now));
Log.d("newpressed: ", String.valueOf(newpressed));
Intent nextScreen = new Intent(getApplicationContext(), AudioRecord.class);
startActivity(nextScreen);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick(View childView, int position) {
File picturelist2 = new File(Environment.getExternalStorageDirectory() +
"/Audio_Recorder_Picture/Pictures", listFolders[position].getName());
if (picturelist2.isDirectory()) {
listFile2 = picturelist2.listFiles();
for (int i = 0; i < listFile2.length; i++) {
Log.i("LIST OF PICTURES: ", String.valueOf(listFile2[i]));
}
}
Intent viewScreen = new Intent(getApplicationContext(), ViewActivity.class);
viewScreen.putExtra("FILE_TAG", listFile2);
startActivity(viewScreen);
}
@Override
public void onItemLongPress(View childView, int position) {
}
}
Code of the adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>` {
public final Activity context;
public final ArrayList<Folder> FOLDERS;
View view;
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
Log.wtf("TAG", "Folders size: " + FOLDERS.size());
return FOLDERS.size();
}
// optimisation of bitmap
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public void loadBitmap(String path, ImageView imageView, int position) {
final String imageKey = String.valueOf(path);
Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
bitmap = decodeSampledBitmapFromResource(path, 100, 100);
imageView.setImageBitmap(bitmap);
// BitmapWorkerTask task = new BitmapWorkerTask(imageView, position);
// task.execute(path);
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public TextView title;
public ImageView image1;
ImageView image2;
ImageView image3;
ImageView image4;
ImageView image5;
TextView slides;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.item);
image1 = (ImageView) v.findViewById(R.id.icon1);
image2 = (ImageView) v.findViewById(R.id.icon2);
image3 = (ImageView) v.findViewById(R.id.icon3);
image4 = (ImageView) v.findViewById(R.id.icon4);
image5 = (ImageView) v.findViewById(R.id.icon5);
slides = (TextView) v.findViewById(R.id.textView1);
}
}
public MyAdapter(Activity context, ArrayList<Folder> FOLDERS) {
this.context = context;
this.FOLDERS = FOLDERS;
getItemCount();
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
Log.wtf("TAG", "OnCreateViewHolder works!!!");
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.mylist, parent, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Folder folder = FOLDERS.get(position);
holder.image1.setImageResource(R.drawable.placeholder);
holder.image2.setImageResource(R.drawable.placeholder);
holder.image3.setImageResource(R.drawable.placeholder);
holder.image4.setImageResource(R.drawable.placeholder);
holder.image5.setImageResource(R.drawable.placeholder);
ArrayList<String> imgs = folder.getPicturelist();
holder.title.setText(folder.getName());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inSampleSize = 10;
for (int i = 0; i < 5; i++) {
switch (i) {
case 0:
if (imgs.size() > i && imgs.size() != 0) {
loadBitmap(imgs.get(i), holder.image1, position);
} else {
holder.image1.setImageBitmap(null);
// holder.image1.setImageResource(R.drawable.placeholder);
}
break;
case 1:
if (imgs.size() > i && imgs.size() != 0) {
loadBitmap(imgs.get(i), holder.image2, position);
} else {
holder.image2.setImageBitmap(null);
// holder.image2.setImageResource(R.drawable.placeholder);
}
break;
case 2:
if (imgs.size() > i && imgs.size() != 0) {
loadBitmap(imgs.get(i), holder.image3, position);
} else {
holder.image3.setImageBitmap(null);
// holder.image3.setImageResource(R.drawable.placeholder);
}
break;
case 3:
if (imgs.size() > i && imgs.size() != 0) {
loadBitmap(imgs.get(i), holder.image4, position);
} else {
holder.image4.setImageBitmap(null);
// holder.image4.setImageResource(R.drawable.placeholder);
}
break;
case 4:
if (imgs.size() > i && imgs.size() != 0) {
loadBitmap(imgs.get(i), holder.image5, position);
} else {
holder.image5.setImageBitmap(null);
// holder.image5.setImageResource(R.drawable.placeholder);
}
break;
}
}
holder.slides.setText("Количество слайдов: " + imgs.size());
view.setTag(holder);
}
public Bitmap getBitmapFromMemCache(String key) {
return com.example.attracti.audiorecorderpicture.FirstscreenActivity.mMemoryCache.get(key);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myproject">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:name=".model.App"
>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<activity android:name=".activities.AudioRecord"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
<activity android:name=".activities.FirstscreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.ViewActivity">
</activity>
</application>
</manifest>
Upvotes: 2
Views: 130
Reputation: 3766
If you are going back to first activity with intent, it will create the activity again, as expected. To prevent this, you can add this to manifest:
<activity android:name=".activities.FirstscreenActivity"
android:launchMode= "singleInstance"
...
singleInstance
will create activity only once.
Use finish()
when you want to go back.
Upvotes: 2