Please i will like to have a method/function in a separate class
that will enable me to go to next activity or fragment class when i call it
here is my little try of code
inside my go_to.class i have this below
for activity
void goToActivity(Activity t){
startActivity(new Intent(this, t.class));
}
void goToFragment(Fragment f,package.com.R l){
getSupportFragmentManager().beginTransaction().add(l,new f).commit();
}
and to use example
goToActivity(mynextactivity.class);
goToFragment(mynextfragment.class,R.id.fragment_layout);
Any help is welcome
From non activity class you can create a function it like this:
public static void goToHomeActivity(Context context)
{
Intent i = new Intent(context, ActivityDashboard.class);
context.startActivity(i);
}
Then call it like this MyUtils.goToHomeActivity(context);
Alternatively, from activity class like this:
private void goToHomeActivity()
{
Intent i = new Intent(getBaseContext(), ActivityDashboard.class);
context.startActivity(i);
}`
Then call it like this goToHomeActivity();
I later solve it this way after many trial:
What i am trying to achive is to have a function to call new activity class and fragment when a button is clicked on at runtime
void goToActivity(Class t){
startActivity(new Intent(this, t));
}
my where i have been getting the fragment concept wrong is fragment is not indepent activity class, so to overcome fragment need anactivity parent or root layout to hold the fragment layout that is you can never start fragment instance alone like activity which is very big issure for begginers to understand
void goToFragment(Fragment f){
fragmentTransaction.replace(R.id.your_layout, f);
fragmentTransaction.addToBackStack(null);
}
or if you cannot determine your xml layout you can write it like this:
void goToFragment(int l,Fragment f){
fragmentTransaction.replace(l, f);
fragmentTransaction.addToBackStack(null);
}
Thank you all for your help
Related
I have MainActivity with BottomNav which includes 3 Fragments A, B, and C.
From Fragment A, I can go to Activity A and from, Activity A I can go to Activity B.
Now I want to return to specifically Fragment A from Activity B by pressing a Back button while skipping Activity A.
I've tried Intent.FLAG_ACTIVITY_CLEAR_TASK in Activity A before starting Activity B and also explicitly starting an Intent to MainActivity from Activity B but it's not giving the result I want and of course it doesn't seem like an efficient way to do it. How do I return back to the fragment?
Case 1
You need to finish Activity A after launching intent.
Intent intent = new Intent(ActivityB.this,ActivityA.class);
startActivity(intent);
finish();
case 2
If you want to come back on Activity A at some point then what you need is passing some flag from Activity B to A. And run this code at the beginning of your activity A.
if(extras.getBoolean("NameOfFlag", false))
{
ActivityA.finish();
}
send flag by using this code.
Intent i = new Intent(ActivityB.this, ActivityA.class);
i.putExtra("NameOfFlag", true);
startActivity(i);
finish();
Remember to finish() Activity when you don't need to come back. In this case ActivityB .
Maybe it is not the ideal solution but should work
In Activity B
#Override
public void onBackPressed() {
Intent i = new Intent(ActivityB.this, MainActivity.class);
i.putExtra("cameFromActivityB", true);
startActivity(i);
}
In Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent.getExtras();
if(extras.getBoolean("cameFromActivityB", false))
{
loadFragmentA();
}
}
// Inside the onCreate on every Activity://
getSupportActionBar().setTitle("name of the Activity here");
getSupportActionBar().setDisplayHomeAsUpEnable(true);
//then go to the manifest file in //
//before that closing bracket
set the order of your Activities; example
(you have three activities,the Main Activity,Activity2,Activity3)//
<activity android:name=".Activity2"
android:parentActivityName=".MainActivity"></activity>
What you can do is when you start your ActivityB from ActivityA just finish your activityA by finish() method.
Intent intent = new Intent(ActivityB.this,ActivityA.class);
startActivity(intent);
finish();
By doing this you will not be redirected to ActivityA instead, you will be directy redirected to your main activity when you press back button from your activityB.
I am Calling a function(present in fragment) from activity when back button is pressed
public void onBackPressed() {
new Home().show(Home.home_list,app.this);
}
Home is fragment of app activity
In show function I am calling an intent
public void show(final ArrayList<tile_data> data, final Activity activity) {
startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
}
In doing so it gives me error : java.lang.IllegalStateException: Fragment Home{5deabab} not attached to Activity
My conclusion is that app is using show as function so it doesn't know about home.
So my question is how to call an "intent" present in some function in some activity or fragment from other activity??
Looks like startActivity is not being called in the right scope. Try specifying the scope and calling it like this: activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class)
Why don't you move the Show() function to the Activity? And for the titles create a getter function in the Home fragment, getTitles(). Now, when the event occurs trigger the Show() function directly in the activity passing (*HomeFragment*.getTitle() , *Activity*.this). Are your titles static? Because you are directly calling new Home(). If that's the case there won't be any issue in calling Show() function in the activity. You can explain your requirements in detail if this isn't up to par.
public void show(final ArrayList<tile_data> data, final Activity activity) {
activity.startActivity(new Intent(activity, Chat_topic_layout_for_user_group.class));
}
In an activity, I have created a AsyncTask after hiding the activity:
this.moveTaskToBack(true);
(new MyTask(this)).execute();
To show a dialog in the task (in onPostExcecute), I want to bring the activity to front:
alertDialog.show();
Intent intent = new Intent(mainActivity, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivity.getBaseContext().startActivity(intent);
But a new instance of the main activity is created and shown on top of the dialog, although the application was still running (the activity has also a dialog style Theme.Dialog). How should I fix this?
Edit: According to javadoc, this code always recreates the activity and doesn't bring its previous instance to front, since startActivity is called from outside of an Activity Context.
How about adding a new piece of information to that intent, and catching it in onCreate()?
What I mean is something like this:
public class MainActivity extends Activity {
public static final String WANT_DIALOG_EXTRA = "WANT_DIALOG_EXTRA";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().hasExtra(WANT_DIALOG_EXTRA)) {
// create and show dialog
}
}
}
Then when you create your intent, add one more line like this:
intent.putExtra(MainActivity.WANT_DIALOG_EXTRA, true);
Usually you would start an activity with something like the following:
private void llMenuSettings_Click() {
this.runOnUiThread(new Runnable() {
public void run() {
Intent intent = new Intent(XXXXXXXX.this, View_Settings.class);
startActivity(intent);
XXXXXXXX.this.finish();
}
});
}
However I am extending a LinearLayout:
public class MenuContainer extends LinearLayout {
}
Therefore I have run into a couple of issues:
How would you know what XXXXXXXX is in the example as the LinearLayout can be included in multiple Layouts?
startActivity() does not exist in a LinearLayout class?
There are a few similar questions but none that fully answer the above scenario therefore hoping this can be answered and help others in the future?
Inside your LinearLayout extending class, you could create the following method:
public void launch() {
Intent i = new Intent(getContext(), YourActivity.class);
getContext().startActivity(i);
}
Use the getContext() method inside your customview to retrieve the Context and start a new Activity.
I am very new with android development. My app has a lot of views/Activity and user can jump from one view to another depending his/hers inputs. so i thought of creating an interface IView which will have a function
void openNewView(Class viewClass);
and the function in the view class would look something like this
public void openNewView(Class viewClass)
{
Intent intent = new Intent(this, viewClass.class);
startActivity(intent);
}
The whole idea is that my controller can listen for user inputs and then call openNewView as per the requirement.
The issue that i am facing is with java not accepting a parameter of type Class
What is it that i am doing wrong here. is there a work around what i am trying to achieve.
One good approach would be extend all your activities from an abstract Activity like this:
public abstract class BaseActivity {
//....
public void openNewView(Class viewClass) {
Intent intent = new Intent(this, viewClass.class);
startActivity(intent);
}
}
Doing this way you don't have to copy your code in every activities and keep code clean.
try by adding current Context param in openNewView method as:
void openNewView(Class viewClass,context);
and in your function :
public void openNewView(Class viewClass,Context context)
{
Intent intent = new Intent(context, viewClass.class);
context.startActivity(intent);
}