从Activity to Fragment返回

时间:2016-06-06 09:48:10

标签: android-fragments

我有一个MainActivity在视图分页器tab1 tab2 tab3中有三个片段,当我点击tab2时,它会带我到ActivityB,在ActivityB的背面按我希望它带我到tab2,而不是主要活动。 请问我该如何实现呢?

TrendFragment

.Rainbow {
background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}

}

TweetListActvity

public class TrendsFragment extends Fragment implements TrendsAdapter.EventListener{

private RecyclerView recyclerView;
protected Realm realm;

public SwipeRefreshLayout refresh;

public TrendsAdapter adapter;

private LinearLayoutManager linearLayoutManager;


@Bind(R.id.landingPage)
public ViewGroup viewGroup;

RealmChangeListener realmChangeListener = new RealmChangeListener() {
    @Override
    public void onChange() {
        adapter.swapData(getPostsFromDb());
    }
};


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    realm = Realm.getDefaultInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getActivity().getWindow().setSharedElementExitTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.postsinfotransition));
    }
    ButterKnife.bind(getActivity());


    TrendsAPIHelper.getTrendsPosts(getActivity());

    /* Used when the data set is changed and this notifies the database to update the information */
    realm.addChangeListener(realmChangeListener);

   /* Animation animation;
    animation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
    viewGroup.startAnimation(animation);*/
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.list_fragment, container, false);
    refresh = (SwipeRefreshLayout) v.findViewById(R.id.refresh);
    refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refresh.setRefreshing(false);
            TrendsAPIHelper.getTrendsPosts(getActivity());
        }
    });
    setRefreshing(true);
    initView(v);
    return v;
}


@Override
public void onPause() {
    super.onPause();
    EventBusSingleton.unregister(this);

}

@Override
public void onResume() {
    super.onResume();
    EventBusSingleton.register(this);
}

@Override
public void onDestroy() {

    if (realm != null) {
        realm.close();
    }

    super.onDestroy();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

/*Initially load the view with information available in the database till the new data is fetched.
* The reason being, a user doesn't have to wait for the data to be previewed in the Screen if there's a slow connection
* or some server error. At this point, a user will mostly have some data presented.
*/
private void initView(View v) {
    RealmResults<Trends> realmResults = getPostsFromDb();
    recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);
    adapter = new TrendsAdapter(getActivity(), realmResults, true);
    adapter.setEventListener(this);
    recyclerView.setAdapter(adapter);
}

@Subscribe
public void onPostSuccess(TrendsAPIHelper.PostsInfoSuccess postInfo) {
    setRefreshing(false);
}

public RealmResults<Trends> getPostsFromDb() {
    RealmResults<Trends> realmResults = realm.where(Trends.class).findAll();
    return realmResults;
}

/* Present user with some error message when there's an issue while retrieving data */
@Subscribe
public void onPostFailure(TrendsAPIHelper.PostsInfoFailure error) {
    setRefreshing(false);
    displaySimpleConfirmSnackBar(recyclerView, error.getErrorMessage());
}

/* Go to the next activity with some values to be presented.
* Since the information is not very large, it can just be sent using Intent-Extras or we can just pass and Item ID
* to the next screen and that ID wil be used to fetch remaining items
*/
@Override
public void onItemClick(View view, Trends postsData) {

    ActivityOptionsCompat optionsCompat = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setTransitionName("xyz");
        optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), view, view.getTransitionName());
    }

    Intent intent = new Intent(getActivity(), TweetsListActivity.class);
    intent.putExtra(TweetsActivity.ARG_SEARCH_REQUEST, postsData.getQuery());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
       // startActivity(intent, optionsCompat.toBundle());
        getActivity().startActivityForResult(intent, 1);
    }

}


public void setRefreshing(final boolean refreshing) {
    refresh.post(new Runnable() {
        @Override
        public void run() {
            refresh.setRefreshing(refreshing);
        }
    });
}
/*
 * Refresh helpers
 */
public boolean isRefreshing() {
    return refresh.isRefreshing();
}

/*Default Snackbar for notifying user with some information*/

public void displaySimpleConfirmSnackBar(View container, String msg) {
    // TODO: There is no design yet for error display.  Update this when that is available.
    Snackbar.make(container, msg, Snackbar.LENGTH_INDEFINITE)
            .setActionTextColor(getResources().getColor(R.color.colorAccent))
            .setAction(android.R.string.ok, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            }).show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    getActivity();
    if(requestCode == 1 && resultCode == Activity.RESULT_OK) {
        //some code
      }
  }

}

0 个答案:

没有答案