TabActivity,Activities containing fragments - android

I have 5 tabs.Each tab contain multiple activities and each activity contain multiple fragments.
For eg:Navigation needed is In tab1 - Activity1 - Fragment1_Activity1 - Fragment2_Activity1 - Activity2 - Fragmnet1_Activity2
I used ActivityGroup to show Activity2 inside the Tabs.
ActivityGroupClass:
public class CouponsActivityGroup extends ActivityGroup {
private Stack<String> stack;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (stack == null) stack = new Stack<String>();
//start default activity
push("FirstStackActivity", new Intent(this, CouponsContianer.class));
}
#Override
public void finishFromChild(Activity child) {
pop();
}
#Override
public void onBackPressed() {
pop();
}
public void push(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
stack.push(id);
setContentView(window.getDecorView());
}
}
public void pop() {
if (stack.size() == 1) finish();
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity(stack.pop(), true);
if (stack.size() > 0) {
Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
Window newWindow = manager.startActivity(stack.peek(), lastIntent);
setContentView(newWindow.getDecorView());
}
}
}
From Fragment2_Activity1 am calling Activity2 using the following code:
Intent intent = new Intent();
intent.setClass(getParent(), RelatedItemListActivity .class);
CouponsActivityGroup activityStack = (CouponsActivityGroup) getParent();
activityStack.push("SecondStackActivity", intent);
Issue am facing now is
I want to set a transition animation for Activity2
On pressing back from Activity2 - last Fragment state in Activity1(Fragment2_Activity1) need to be maintained.
How to achieve this?

Fragments are some kind of replacement of old ActivityGroups and shouldn't be used together. Try to implement your application using only one activity and as many fragments as you need.
Answering your questions:
FragmentManager helps you to set an animation. See FragmentTransaction.setCustomAnimations .
Use addToBackStack method to support back button proper way.

Related

Refreshing fragments on filter selection from parent activity

I have this scenario where I have An Activity(A) which has 2 tabs (ie. two fragments FA1,FA2) . The appBar in Activity A has a filter button which opens different Activities(B & C) depending on which tab is selected.
this has been handled by the following code in A.
toolbar.findViewById(R.id.filterimage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = null;
if (tabLayout.getSelectedTabPosition() == 1) {
i = new Intent(A.this, B.class);
} else {
i = new Intent(A.this, C.class);
}
startActivityForResult(i, 221);
}
});
all the api hits to show data in two fragments are made in the respective fragments. Filter which has been selected from Activity B or C is returned to activity A .
Now depending on the filters selected I want to refresh the fragment(FA1 or FA2) again from it's parent activity ie. A.
But I am unable to do so .
How can I do this?
#Override
protected void onResume() {
super.onResume();
// Where currentFragment is the fragment you want to refresh
getSupportFragmentManager()
.beginTransaction()
.detach(currentFragment)
.commitNowAllowingStateLoss();
getSupportFragmentManager()
.beginTransaction()
.attach(currentFragment)
.commitAllowingStateLoss();
}
Here you have to check particular case when it comes from B or C activity, otherwise don't refresh.
Use different request code for both like below:
toolbar.findViewById(R.id.filterimage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = null;
if (tabLayout.getSelectedTabPosition() == 1) {
i = new Intent(A.this, B.class);
startActivityForResult(i, 221);
} else {
i = new Intent(A.this, C.class);
startActivityForResult(i, 222);
}
}
});
Then depends on request code get result and update fragments data.

Android Studio - Can a fragment be modified after it's created?

I have an Activity that has a section where a Fragment is shown. A button is clicked on the Fragment and a new Activity launches with some buttons. Depending on which button is clicked, that activity closes and the Fragment should be updated accordingly but I'm having trouble accessing the views on the Fragment to edit them.
It seems that in the onResume() of the Fragment, I'm unable to access the view using getView(). I've also tried passing in the view from the Activity that the Fragment lives in, but that also returned null.
So I'm wondering if Fragments are static after onCreateView() is called?
SecondActivity calling back into MainActivity:
#Override
protected void onResume() {
super.onResume();
rvAdapter.setOnItemClickListener(new RVAdapter.MyClickListener() {
#Override
public void onItemClick(View v, int position) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("key", value);
startActivity(intent);
}
});
}
MainActivity handling the call back and calling a function in the Fragment:
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String value = intent.getStringExtra("key");
if (value != null) {
((ExampleFragment) exampleFragment).updateFragment(value);
}
}
Fragment attempting to update itself:
public void updateFragment(String value) {
TextView textView = (TextView) getView().findViewById(R.id.profile_text_view);
if (value != null) {
textView.setText(value);
}
}
And the result would be NPE on getView() call in the Fragment. I've tried passing in the view from MainActivity via: exampleFragment.getView() but also no luck.

TabGroupActivity + Prevent reload of an activity onBackPressed

This is the code of my TabGroupActivity. Here On any of the particular tab Tab B-> then It will launch Its child activity Class C -> then on click of any button It will again calls the child activty Class D
Now from class D whenever I pressed back button, then It will again reload the child activity class C. From class C, when I press the back button, again It will reload the child activity.
Please help me into this.. I dont want the previous activity to reload.
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
if (mIdList == null) mIdList = new ArrayList<String>();
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {#link LocalActivityManager#destroyActivity} on the child activity
* and starts the previous activity.
* If the last child activity just called finish(),this activity (the parent),
* calls finish to finish the entire group.
*/
#Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
Log.i("Stack,finishFromChild", ""+mIdList);
int index = mIdList.size()-1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index); index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
lastIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
/**
* Starts an Activity as a child Activity to this.
* #param Id Unique identifier of the activity to be started.
* #param intent The Intent describing the activity to be started.
* #throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
/**
* The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
* from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
*/
/**
* If a Child Activity handles KeyEvent.KEYCODE_BACK.
* Simply override and add this method.
*/
#Override
public void onBackPressed () {
int length = mIdList.size();
if ( length > 1) {
Log.i("Stack,onback", ""+mIdList);
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
Log.i("Activity in current",""+current);
current.finish();
}
else
{
if(length==1)
{
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
Log.i("Activity in current",""+current);
current.finish();
}
}
}

TabGroupActivity - startChildActivity - not working

I have been using TabActivity and I want the tab to display on every child activity. I have flow like this MainActivity(TabActivity) -> TabGroupActivity1(TabGroupActivity) -> Activity1 -> Activity2
Now i want to redirect on Activity2 only if the flag is true. so that my code for that is something like bellow.
TabGroupActivity
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {#link LocalActivityManager#destroyActivity} on the child activity
* and starts the previous activity.
* If the last child activity just called finish(),this activity (the parent),
* calls finish to finish the entire group.
*/
#Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size()-1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index); index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
/**
* Starts an Activity as a child Activity to this.
* #param Id Unique identifier of the activity to be started.
* #param intent The Intent describing the activity to be started.
* #throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
/**
* The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR
* from calling their default KeyEvent.KEYCODE_BACK during onKeyDown.
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Overrides the default implementation for KeyEvent.KEYCODE_BACK
* so that all systems call onBackPressed().
*/
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* If a Child Activity handles KeyEvent.KEYCODE_BACK.
* Simply override and add this method.
*/
#Override
public void onBackPressed () {
int length = mIdList.size();
if ( length > 1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
current.finish();
}
}
}
now code for TabGroupActvity1
public class TabGroupActyvity1 extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,Activity1.class));
}
}
now in Activity1
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if(flag){
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
Intent previewMessage = new Intent(parentActivity, Activity2.class);
parentActivity.startChildActivity("Activity2", previewMessage);
}else{
setContentView(R.layout.row);
//...
}
}
this is not working,
the same code the one in bellow works in some click event but not working in my case
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
Intent previewMessage = new Intent(parentActivity, Activity2.class);
parentActivity.startChildActivity("Activity2", previewMessage);
please give some suggestion how to do this.
have I explain the problem well... do I need to add some more details?
Okay, I found other alternative.
Instead of...
checking flag in the child activity and redirecting on different page.
I am checking the flag in the parnt activity Like this
if (getLNApplication().isLogin()) {
startChildActivity("Report", new Intent(this, ReportActivity.class));
}else{
startChildActivity("LogIn", new Intent(this, LoginActivity.class));
}
and from LoginActivity on Successful login i am Starting ReportActivity like bellow
parentActivity.startChildActivity("EditActivity", new Intent(getParent(), ReportActivity.class));
and I also handle the back press as of I don't want user to go back on login page again. I handle the back key in TabGroupActivity
like this
#Override
public void onBackPressed() {
int length = mIdList.size();
if (length > 1) {
Activity current = getLocalActivityManager().getActivity(
mIdList.get(length - 1));
// Added code to disable back press only for the ReportActivity
if(current instanceof ReportActivity){
Log.i("TabGroup", "I am instance of ReportActivity" );
return;
}
current.finish();
}
}

ActivityGroup inside TabHost does not show content after first run

I have issue with ActivityGroup. My app has 4 tabs, 2 of which has ActivityGroup and 2 more simple activity. The problem is that after first run of app content is shown properly, and when leave app through back button and return, tabs with activity group dont shown any content, including menus. While tabs with simple activity work properly.
D'you have any ideas?
Ok, some sort of code)
Setting this tab:
private TabSpec getFrontPageTab() {
Intent intent = new Intent(context, ActivityGroupHome.class);
return tabHost
.newTabSpec("home")
.setIndicator(
getTabView(R.drawable.tabbar_home, "str_home"))
.setContent(intent);
}
ActivityGroupHome:
public class ActivityGroupHome extends ActivityGroupBase {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ActivityUtils activityUtils = ActivityUtils.getInstance(this);
activityUtils.addActivityGroup("Home", this);
activityUtils.startHomeActivity("Home");
}
}
Methods from ActivityUtils:
public void startHomeActivity(String activityGroupName) {
if (activityGroupName != null) {
startHomeActivityForActivityGroup(activityGroupName);
} else {
Intent intent = new Intent(context, AsyncMainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
}
private void startHomeActivityForActivityGroup(String activityGroupName) {
ActivityGroupListItem activityGroupItem = activityGroups
.findGroupByName(activityGroupName);
if (activityGroupItem != null) {
Intent intent = new Intent(activityGroupItem.activityGroup,
AsyncMainActivity.class);
intent.putExtra(ACTIVITY_GROUP_NAME, activityGroupItem.name);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View view = activityGroupItem.activityGroup
.getLocalActivityManager().startActivity("Home", intent)
.getDecorView();
activityGroupItem.activityGroup.setContentView(view);
activityGroupItem.stack.add("Home");
}
}

Categories

Resources