androidtabsactivitygroup

android: How can i set backpress on TabActivity ? where one tab contain only two activity?



Solution

  •          - this solve ...use this Code..
        1)OffersActivityGroup  add one Activity to add your root activity...
    
            public class OffersActivityGroup extends ActivityGroup {
    
                public static OffersActivityGroup group;
                private ArrayList<View> history;
    
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    this.history = new ArrayList<View>();
                    group = this;
    
                    // Start the root activity withing the group and get its view
                    View view = getLocalActivityManager().startActivity(
                            "OffersActivity",
                            new Intent(this, OffersActivity.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                            .getDecorView();
    
                    // Replace the view of this ActivityGroup
                    replaceView(view);
    
                }
    
                public void replaceView(View v) {
                    // Adds the old one to history
                    history.add(v);
                    // Changes this Groups View to the new View.
                    setContentView(v);
                }
    
                public void back() {
                    if (history.size() > 0) {
                        history.remove(history.size() - 1);
                        setContentView(history.get(history.size() - 1));
                    } else {
                        finish();
                    }
                }
    
                @Override
                public void onBackPressed() {
                    OffersActivityGroup.group.back();
                    return;
                }
    
            }
    
    
        2)OffersActivity
    
            public class OffersActivity extends Activity implements OnItemClickListener {
    
            CustomArrayAdapter adapter;
            ListView lvProducts;
            ArrayList<Products> productObj;
            Products product;
    
            // View rootView;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.product_list);
    
                init();
    
                // hideFB();
                // hideTwitter();
                setTitle(getResources().getString(R.string.title_offer));
                // API Call
    
                if (Global.isNetworkAvailable(OffersActivity.this)) {
                    new ProductAsyncTask().execute(Constants.url);
                } else {
                    Intent intent = new Intent(OffersActivity.this,
                            OfflineActivity.class);
                    startActivity(intent);
                }
            }
    
            @Override
            protected void onResume() {
                super.onResume();
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
            }
    
            private void init() {
                productObj = new ArrayList<Products>();
                lvProducts = (ListView) findViewById(R.id.lv_products);
                lvProducts.setOnItemClickListener(this);
            }
    
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    
                Intent intent = new Intent(OffersActivity.this,
                        OffersDetailActivity.class);
                intent.putExtra("product", (Products) productObj.get(position));
    
    
                View view = OffersActivityGroup.group
                        .getLocalActivityManager()
                        .startActivity("OffersDetailActivity",
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                        .getDecorView();
                OffersActivityGroup.group.replaceView(view);
    
            }
    
            public void loadData() {
                adapter = new CustomArrayAdapter(OffersActivity.this,
                        R.layout.product_list_row, productObj);
    
                lvProducts.setAdapter(adapter);
            }
    
            // @Override
            // public void onBackPressed() {
            // OffersActivity.group.back();
            // return;
            // }
    
            public class ProductAsyncTask extends
                    AsyncTask<String, String, ArrayList<Products>> {
    
                String responseString = null;
                ProgressDialog progressDialog;
    
                public ProductAsyncTask() {
                    super();
                    progressDialog = new ProgressDialog(getParent());
                    progressDialog.setCancelable(false);
                }
    
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                }
    
                @Override
                protected ArrayList<Products> doInBackground(String... params) {
    
                    responseString = readJSONSFeed(params[0]);
                    if (responseString != null) {
                        return processUserListJSON(responseString);
                    } else {
                        return null;
                    }
                }
    
                @Override
                protected void onPostExecute(ArrayList<Products> result) {
                    super.onPostExecute(result);
                    if (result.size() == 0 || result == null) {
                    }
                    progressDialog.dismiss();
                    loadData();
    
                }
    
                private String readJSONSFeed(String url) {
    
                    StringBuilder stringBuilder = new StringBuilder();
    
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_OFFER);
    
    
    
                    try {
                        HttpResponse response = httpClient.execute(httpGet);
                        StatusLine statusLine = response.getStatusLine();
    
                        int statuscode = statusLine.getStatusCode();
    
                        if (statuscode == 200) {
                            HttpEntity httpEntity = response.getEntity();
                            InputStream inputStream = httpEntity.getContent();
    
                            BufferedReader buffer = new BufferedReader(
                                    new InputStreamReader(inputStream));
    
                            String line = null;
                            if ((line = buffer.readLine()) != null) {
                                stringBuilder.append(line);
                            }
                            // buffer.close();
                            inputStream.close();
                        } else {
                        }
    
                    } catch (Exception e) {
                    }
                    return stringBuilder.toString();
                }
    
                private synchronized ArrayList<Products> processUserListJSON(
                        String response) {
    
                    try {
                        JSONObject json = new JSONObject(response);
                        JSONArray Records = json
                                .getJSONArray(Constants.JSON_OBJECT_DATA);
                        for (int i = 0; i < Records.length(); i++) {
                            JSONObject Record = Records.getJSONObject(i);
    
                            String tProductID = Record.getString("product_id");
                            String tTitle = Record.getString("title");
                            String tImage = Record.getString("image");
                            String tDetails = Record.getString("short_description");
                            String tLongDetails = Record.getString("long_description");
                            String tOffer = Record.getString("offer");
                            String tActive = Record.getString("active");
                            String tPromoText = Record.getString("promotion_text");
                            String tProminent = Record.getString("prominent");
    
                            product = new Products(tProductID, tTitle, tDetails,
                                    tLongDetails, tImage, tOffer, tActive, tPromoText,
                                    tProminent);
    
                            if (product == null) {
                            } else {
                                productObj.add(product);
                            }
    
                        }
    
                    } catch (JSONException ex) {
                        System.out.println(ex);
                        return null;
                    }
    
                    return productObj;
                }
    
            }
    
        }
    2)OfferDetailsActivity
    
        public class OffersDetailActivity extends Activity implements
            OnClickListener, OnItemSelectedListener {
        Gallery gallery;
        TextView prod_descri, productTitle1, productTitle2;
        ImageAdapter imageAdapter;
        ImageView leftArrow, rightArrow;
        private int selectedImagePosition = 0;
        private List<Drawable> drawables;
        String title, url, title1;
        Bundle b;
        Products product;
        ProductDetails pDetails;
        ArrayList<ProductDetails> productList;
        Button btnFacebook, btnTwitter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.products_details);
            init();
            setTitle(getResources().getString(R.string.title_offer));
            b = getIntent().getExtras();
            product = (Products) b.getSerializable("product");
            if (b != null) {
                if (Global.isNetworkAvailable(OffersDetailActivity.this)) {
                    new ProductAsyncTask().execute(Constants.url,
                            product.product_id);
                } else {
    
                    Intent intent = new Intent(OffersDetailActivity.this,
                            OfflineActivity.class);
                    startActivity(intent);
                }
            } else {
            }
        }
    
    //  @Override
    //  public void onBackPressed() {
    //      OffersActivity.group.back();
    //      // super.onBackPressed();
    //
    //  }
    
        private void init() {
            productList = new ArrayList<ProductDetails>();
            gallery = (Gallery) findViewById(R.id.product_gallery);
            prod_descri = (TextView) findViewById(R.id.product_desc);
            productTitle1 = (TextView) findViewById(R.id.productTitle1);
            productTitle2 = (TextView) findViewById(R.id.productTitle2);
    
            leftArrow = (ImageView) findViewById(R.id.left_arrow_imageview);
            rightArrow = (ImageView) findViewById(R.id.right_arrow_imageview);
    
            btnFacebook = (Button) findViewById(R.id.btnFB);
            btnFacebook = (Button) findViewById(R.id.btnTwitter);
            //btnFacebook.setOnClickListener(this);
            // btnTwitter.setOnClickListener(this);
    
            leftArrow.setOnClickListener(this);
            rightArrow.setOnClickListener(this);
            gallery.setOnItemSelectedListener(this);
        }
    
        @Override
        public void onClick(View v) {
    
            switch (v.getId()) {
            case R.id.left_arrow_imageview:
    
                if (selectedImagePosition > 0) {
                    // leftArrow.setVisibility(View.VISIBLE);
                    --selectedImagePosition;
                } else if (selectedImagePosition == 0) {
                    leftArrow.setVisibility(View.GONE);
                    rightArrow.setVisibility(View.VISIBLE);
                }
                gallery.setSelection(selectedImagePosition, true);
    
                break;
            case R.id.right_arrow_imageview:
                if (selectedImagePosition < drawables.size() - 1) {
                    // rightArrow.setVisibility(View.VISIBLE);
                    ++selectedImagePosition;
                } else if (selectedImagePosition == drawables.size() - 1) {
                    rightArrow.setVisibility(View.GONE);
                    leftArrow.setVisibility(View.VISIBLE);
                }
    
                gallery.setSelection(selectedImagePosition, true);
    
                break;
    
    
    
            default:
                break;
            }
    
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {
    
            selectedImagePosition = position;
    
            productTitle1.setText(product.title);
            productTitle2.setText(productList.get(position).title);
            prod_descri.setText(productList.get(position).description);
    
            if (selectedImagePosition > 0
                    && selectedImagePosition < drawables.size() - 1) {
                leftArrow.setVisibility(View.VISIBLE);
                rightArrow.setVisibility(View.VISIBLE);
            } else if (selectedImagePosition == 0) {
                leftArrow.setVisibility(View.GONE);
            } else if (selectedImagePosition == drawables.size() - 1) {
                rightArrow.setVisibility(View.GONE);
            }
            // setSelectedImage(selectedImagePosition);
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    
        // AsyncTask
    
        public class ProductAsyncTask extends
                AsyncTask<String, String, ArrayList<ProductDetails>> {
    
            private Context getDialogContext() {
                Context context;
                if (getParent() != null)
                    context = getParent();
                else
                    context = OffersDetailActivity.this;
                return context;
            }
    
            String responseString = null;
            ProgressDialog progressDialog;
    
            public ProductAsyncTask() {
                super();
                // progressDialog = new ProgressDialog(ProductDetailsActivity.this);
                progressDialog = new ProgressDialog(getDialogContext());
                progressDialog.setCancelable(false);
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
    
            @Override
            protected ArrayList<ProductDetails> doInBackground(String... params) {
    
                responseString = readJSONSFeed(params[0], params[1]);
    
                if (responseString != null) {
                    return processProductDetailsJSON(responseString);
                } else {
                    return null;
                }
            }
    
            @Override
            protected void onPostExecute(ArrayList<ProductDetails> result) {
                super.onPostExecute(result);
                if (result.size() == 0 || result == null) {
                }
                progressDialog.dismiss();
    
                drawables = createDrawables(result);
                imageAdapter = new ImageAdapter(OffersDetailActivity.this,
                        drawables);
                if (drawables.size() > 0) {
    
                    gallery.setSelection(selectedImagePosition, true);
                }
    
                if (drawables.size() == 1) {
                    leftArrow.setVisibility(View.GONE);
                    rightArrow.setVisibility(View.GONE);
                }
    
                gallery.setAdapter(imageAdapter);
    
            }
    
            private List<Drawable> createDrawables(ArrayList<ProductDetails> result) {
    
                List<Drawable> drawablesUrl = new ArrayList<Drawable>();
                try {
                    for (ProductDetails objProDetails : result) {
                        drawablesUrl.add(Global
                                .getBitmapFromURL(objProDetails.image_path));
                        // drawablesUrl.add(loadImageFromURL(objProDetails.image_path));
                    }
                    return drawablesUrl;
    
                } catch (Exception e) {
                    return null;
                }
    
            }
    
            @SuppressWarnings("unused")
            private Drawable loadImageFromURL(String image_path) {
                try {
                    InputStream is = (InputStream) new URL(url).getContent();
                    Drawable d = Drawable.createFromStream(is, "src name");
                    return d;
                } catch (Exception e) {
                    return null;
                }
            }
    
            private String readJSONSFeed(String url, String productID) {
    
                StringBuilder stringBuilder = new StringBuilder();
    
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_PRODUCT_IMAGE
                        + productID + Constants.REQ_FOR_OFFER);
    
    
    
                try {
                    HttpResponse response = httpClient.execute(httpGet);
                    StatusLine statusLine = response.getStatusLine();
    
                    int statuscode = statusLine.getStatusCode();
    
                    if (statuscode == 200) {
    
                        HttpEntity httpEntity = response.getEntity();
                        InputStream inputStream = httpEntity.getContent();
    
                        BufferedReader buffer = new BufferedReader(
                                new InputStreamReader(inputStream));
    
                        String line = null;
                        if ((line = buffer.readLine()) != null) {
                            stringBuilder.append(line);
                        }
                        // buffer.close();
                        inputStream.close();
                    } else {
                    }
    
                } catch (Exception e) {
                }
                return stringBuilder.toString();
            }
    
            private synchronized ArrayList<ProductDetails> processProductDetailsJSON(
                    String response) {
    
                try {
                    JSONObject json = new JSONObject(response);
                    JSONArray Records = json
                            .getJSONArray(Constants.JSON_OBJECT_DATA);
                    for (int i = 0; i < Records.length(); i++) {
                        JSONObject Record = Records.getJSONObject(i);
    
                        String tTitle = Record.getString("title");
                        String tImagePath = Record.getString("image_path");
                        String tDescription = Record.getString("description");
    
                        pDetails = new ProductDetails(tDescription, tTitle,
                                tImagePath);
    
                        if (pDetails == null) {
                        } else {
                            productList.add(pDetails);
                        }
                    }
    
                } catch (JSONException ex) {
                    System.out.println(ex);
                    return null;
                }
    
                return productList;
            }
    
        }
    
    }