I have a MainActivity housing three Fragments in a view pager tab1 tab2 tab3, when i click on tab2 for instance, it takes me to an ActivityB, on back press of ActivityB i want it to take me to tab2, instead of the MainActivity. please how can i implement that?
TrendFragment
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
}
}
}
TweetListActvity
public class TweetsListActivity extends AppCompatActivity {
public static final String ARG_SEARCH_REQUEST = "tweet";
private String request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweets_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TwitterAuthConfig authConfig = new TwitterAuthConfig("RGj7cK1LtaCBYXpp32HZP7MhW",
"EyZwvfAMSEBRIlVPWN6XDZ2yk4fKSnB1hOBTt4XAlZ8c3c32u5");
Fabric.with(this, new Twitter(authConfig));
Intent intent = getIntent();
request = intent.getStringExtra(ARG_SEARCH_REQUEST);
getSupportActionBar().setTitle(request);
final ListView listView = (ListView) findViewById(R.id.list);
final SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
final SearchTimeline timeline = new SearchTimeline.Builder()
.query(request)
.build();
final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
.setTimeline(timeline)
.build();
listView.setAdapter(adapter);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeLayout.setRefreshing(true);
adapter.refresh(new Callback<TimelineResult<Tweet>>() {
@Override
public void success(Result<TimelineResult<Tweet>> result) {
swipeLayout.setRefreshing(false);
}
@Override
public void failure(TwitterException e) {
}
});
}
});
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (listView.getChildAt(0) != null) {
swipeLayout.setEnabled(listView.getFirstVisiblePosition() == 0 && listView.getChildAt(0).getTop() == 0);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(TweetsListActivity.this, SettingsActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}