This is my code:
Home.java
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Home extends Fragment {
private TextView textView23;
private ImageView addNewSalesInquery;
private LinearLayout addNewSalesButton;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.activity_home,container,false);
//Intializing instance variables
//addNewSalesInquery = (ImageView)findViewById(R.id.add_new_sales_inq);
//textView23 = (TextView)findViewById(R.id.textView23);
//textView23.setSelected(true);
addNewSalesButton = (LinearLayout)view.findViewById(R.id.add_new_sales_btn);
//Registering Listeners
//addNewSalesInquery.setOnClickListener(new AddNewSalesInqury());
addNewSalesButton.setOnClickListener(new AddNewSalesInqury());
return view;
}
//Event Handler for Add New Sales
private class AddNewSalesInqury implements OnClickListener
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_container, new ViewPagerManager(), "fragment").addToBackStack("fragment");
// Start the animated transition.
ft.commit();
}
}
}
ViewPagerManager.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class ViewPagerManager extends Fragment {
private ImageView addNewSalesInqury;
private RelativeLayout salesInquryMainLayout;
private TableRow testEditSales;
private View view;
private static final int ITEMS = 2;
private static ViewPager viewPager;
private MyAdapter pageAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.activity_leads_and_sales_handling,container,false);
intialize();
return view;
}
private void intialize()
{
viewPager = (ViewPager)view.findViewById(R.id.pager);
pageAdapter = new MyAdapter(getFragmentManager());
viewPager.setAdapter(pageAdapter);
}
private class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return ITEMS;
}
@Override
public Fragment getItem(int position) {
if(position==0)
{
return new SalesInqury();
}
else
{
return new NewLead();
}
}
}
public static void setCurrentItem (int item, boolean smoothScroll) {
viewPager.setCurrentItem(item, smoothScroll);
}
//This method will handle menu clicks
public static void onMenuItemClicked(View view) {
if(view.getId()==R.id.menu_add_inquiry)
{
setCurrentItem(2,true);
}
}
}
activity_leads_and_sales_handling.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LeadsAndSalesHandlingActivity"
android:id="@+id/leadsAndSalesHandlingActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</android.support.v4.view.ViewPager>
</RelativeLayout>
I use this to navigate from Home
to ViewPagerManager
. In ViewPagerManager
, if I click the back button, I can go back to Home
. Now if I try to navigate back to the ViewPagerManager
by clicking the virtual button on application, what happens is it simply navigates to a 100% blank white colour screen! What is wrong here?
UPDATE
I think I found the problem. In activity_leads_and_sales_handling.xml there is a ViewPager
and what it should display is the first Fragment
in the ViewPager
. When the back button is clicked, I think this ViewPager
stops working or stop displaying UI due to some reason.