重启Android的所有活动

时间:2015-09-28 16:13:11

标签: android

我想创建自己的应用程序,以便在用户从gridView中删除图像时重新启动它。这意味着,当我打开两个活动时,当用户单击一个按钮时,所有活动都将关闭,之后重新打开,就好像什么也没发生一样。

GridViewAdapter

 public class GridViewAdapter extends BaseAdapter {
ImageView image;
// Declare variables
private Activity activity;
private String[] filepath;
private String[] filename;
CheckableLayout l;
private static LayoutInflater inflater = null;

public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
    activity = a;
    filepath = fpath;
    filename = fname;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return filepath.length;

}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}


public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
    // Locate the TextView in gridview_item.xml
    TextView text = (TextView) vi.findViewById(R.id.text);
    // Locate the ImageView in gridview_item.xml
     image = (ImageView) vi.findViewById(R.id.grid_image);

    // Set file name to the TextView followed by the position
    File file = new File(filepath[position]);
    Picasso.with(activity).load(file).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);


    // Decode the filepath with BitmapFactory followed by the position


    // Set the decoded bitmap into ImageView
  //  image.setImageBitmap(bmp);
    return vi;
}

}

这是我的onActivityResult,在用户从图库中选择他的图像

之后
public void onActivityResult(int requestCode, int resultCode, Intent data) {


    // get image from external storage

    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CODE_EXTERNAL_IMAGE) {
            Uri uri = data.getData();
            InputStream inputStream;
            try {
                inputStream = getContentResolver().openInputStream(uri);
                Bitmap image = BitmapFactory.decodeStream(inputStream);

                ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
                bitmapArray.add(image);

                OutputStream output;
                // Find the SD Card path
                File filepath = Environment.getExternalStorageDirectory();

                int count2 = 0;
                SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
                count2 = settings.getInt("SNOW_DENSITY", 0); //0 is the default value
                String paranteza1 = "(";
                String paranteza2 = ")";
                int count1 = new File("/mnt/sdcard" + nameAlbum).listFiles().length;
                count1 = count1 + 1;
                String nameImage = "Wallpaper_" + count1 + ".jpg";
                for (int j = 0; j < listFile.length; j++) {
                    FileNameStrings[j] = listFile[j].getName();


                    if (FileNameStrings[j].equals(nameImage)) {
                        nameImage = "Wallpaper_" + count1 + paranteza1 + count2 + paranteza2 + ".jpg";
                        count2++;
                        settings = getSharedPreferences("YOUR_PREF_NAME", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putInt("SNOW_DENSITY", count2);
                        editor.commit();
                        for (int i = 0; i < listFile.length; i++) {
                            FileNameStrings[i] = listFile[i].getName();
                            if (FileNameStrings[i].equals(nameImage)) {

                                nameImage = "Wallpaper_" + count1 + paranteza1 + count2 + paranteza2 + ".jpg";
                                count2++;
                                settings = getSharedPreferences("YOUR_PREF_NAME", 0);
                                editor = settings.edit();
                                editor.putInt("SNOW_DENSITY", count2);
                                editor.commit();
                            }
                        }
                    }
                }
                count2++;
                File dir = new File(filepath.getAbsolutePath()
                        + nameAlbum);

                // Retrieve the image from the res folder


                // Create a name for the saved image
                file = new File(dir, nameImage);


                try {
                    inputStream = getContentResolver().openInputStream(uri);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    output = new FileOutputStream(file);

                    // Compress into png format image from 0% - 100%
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
                    output.flush();
                    output.close();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }


        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                    .show();
        } else {
            // Locate the image folder in your SD Card
            file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + nameAlbum);

            if (file.isDirectory()) {
                listFile = file.listFiles();
                // Create a String array for FilePathStrings
                FilePathStrings = new String[listFile.length];
                // Create a String array for FileNameStrings
                FileNameStrings = new String[listFile.length];

                for (int i = 0; i < listFile.length; i++) {
                    // Get the path of the image file
                    FilePathStrings[i] = listFile[i].getAbsolutePath();
                    // Get the name image file
                    FileNameStrings[i] = listFile[i].getName();
                    // Locate the GridView in gridview_main.xml
                    grid = (GridView) findViewById(R.id.gridview);
                    // Pass String arrays to LazyAdapter Class
                    adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);
                    // Set the LazyAdapter to the GridView
                    grid.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }
            }
        }

1 个答案:

答案 0 :(得分:0)

如果我找到了你,那么这应该是完美的:使用带有旗帜的意图。

Intent DeleteVideo = new Intent(getActivity(), Actvity2.class);
DeleteVideo.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(DeleteVideo);
finish;

如果我错过了某些内容,请阅读Tasks and Back Stack