Is that's possible when user open my app to lunch Activity depends on something or lunch another activity not mainActivity if some happened.
My problem is that I have tow activities LogInActivity and BrowseDataActivity inside browseData I have Viewpager uses fragments which means that I can't use fragments instead of activities because you can't have fragments inside fragment.
if user is logged in then start BrowseDataActivity other wise LogInActivity is that possible?
I think I saw some code working around
Useing java script even I could load my views depending on.... but I don't wanna do that or use java scripts I could work around but useing stupid way.
Thanks
Set LoginActivity as main activity and in LoginActivity.onCreate start the other activity if the user is already logged in
OR
Create an other activity (let's call it SplashScreenActivity) with something like this in onCreate :
setContentView(R.layout.myview);
boolean loggedIn = ...;
Intent i;
if(loggedIn)
i = new Intent(this, yourActivity.class);
else
i = new Intent(this, LogInActivity.class);
startActivity(i);
Sorry I cant add code tags because i am on my mobile
Just a modification of the above code it seems you want only two activities only. Lets get the requirements
Only two activites
Show BrowseActivity only if logged in
if not logged in show LoginActivity
Simple
In Browse Activity.
public boolean isLoggedin = prefs.getBoolean("isLoggedIn",false);
...
onCreate(){
if (isLoggedIn)
setContentView(R.layout.browseActivity)
else{
intent i = new intent(this,LoginActivity);
startActivity(i);
}
Now in LoginActivity.
onCreate(){
//Code for loggin in possible conncecting to a server etc... whatever your implementation is
//If successful
SharedPreferences sp = getSharedPreferences("user_Data",MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean("isLoggedIn", true);
ed.commit();
//As the user is now logged in as they got this far...take them to the browse activity!
Intent i = new intent(this,BrowseActivity.class);
startActivity(i);
//else they are not logged in so show a "try again dialog"
}
This complements all of the requirements.
Hope that helps
well i know no one answer this question, but i want to answer my own question to help anyone facing same problem.
Problem
i have LogInActivity and BrowseDataActivity "with Swipe Tabs"
we can write code in LogInActivity check if user loggedIn then show BrowseDataActivity remove LogInActivity from Stack else load LogInActivity views but problem is that Activity Window Animation when user is loggedIn first LogInActivity Window Animation then BrowseDataActivity Window Animation if you could remove this animation every thing is cool but i will solve this problem by using Fragments.
solution
we will have one Activity called MainActivity something like that
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/fragment_container"
android:visibility="gone"></FrameLayout>
</FrameLayout>
,we will have 1 Fragment LogInFragment what ever it's looks like
and BrowseDataActivity's Swipe Tabs as you can see we have viewPager
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isUserLoggedIn()){
setUpSwaipTabs();// setUp Tab and Viewpager
}else{
showLogInFragment(); // load your Fragemtn
//in FrameLayout id=fragment_container and show
// and hide viewpager.
}
}
Related
I've been working on this problem now for several hours but still couldnt figure it out.
I have a MainActivity with several Tabs, which are all linked to their own fragment. The Tabs and their fragments are all working perfectly fine.
Main Activity with different Tabs(Fragments):
When I switch to the second Tab you can see a button.
MainActivity and Tab2(Fragment1):
Through the button "Theorie" you are navigated to the SecondActivity.
SecondActivity:
Now there is the following problem. I would like to return to Tab2 in the MainActivity. But because this is the second fragment of the MainActivity, I always get transfered only to the first Tab "Home" in the MainActivity with the following code.
`
ImageView homebtn = (ImageView) findViewById(R.id.homebtn);
homebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
`
I really don't know, how I can get through the homebtn to the second Fragment "Tab2" instead of the first Fragment "Home" in the MainActivity. When I conducted a research about this problem, I only found solutions how to transfer data from on fragment to another or how to get from the MainActivity to its fragments. I couldn't find any solutions for my specific problem.
If you have any advice for me how to solve this problem, I would be very thankful :)
Greetings
Chris
When you are going to the Second activity, don't finish the first activity. And on home button just use this :-
finish();
Instead of:-
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
You are finishing the first activity in the process and starting a new activity altogether which resets everything.
Hope this helps.
So my title may be hard to follow, but I'll try to clarify and expand on what my issue is below.
I currently have an app that starts its life as a MainActivity with multiple Fragments sitting in a ViewPager.
In the MainActivity, I have Android In-App Billing V3 (library) setup so that the user can pay to remove ads. This works just fine in the MainActivity but my issue arises when moving to another Activity.
The first Fragment the user is presented with upon launching the app, contains a RecyclerView with an ArrayList of items. To get to a sub-Activity from the MainActivity, the user presses a button on one of the items in the RecyclerView, which means that the Intent data used to change Activities is contained within the RecyclerViewAdapter.
My issue is that once my app knows that the user has paid to remove ads, I want the app to also remove ads in all sub-Activities as well.
I don't know how to pass this info (that the "Remove Ads" in-app has been purchased) from Activity -> sub-Activity, when sub-Activity is launched through the RVAdapter instead.
So my question is: How would I pass data from MainActivity -> RVAdapter -> Sub-Activity?
Or is there an even better, more efficient way of passing this data along without using Intents? Do let me know!
Did my description of the issue make sense? I hope so! Otherwise let me know how I might clarify it! If you need me to paste in any code, let me know as well.
Thanks for any of your help!
you can use EventBus (greenrobot) nice library for send event this linke
to send evnts
after add library put below method to your main activity:
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event)
don't forget about Register and unregister subscriber, do it like this :
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
finally post your event from everywhere like your subactivity :
EventBus.getDefault().postSticky(new MessageEvent());
Notice:I add postSticky(); to cache data on memory ,Then the sticky event can be delivered to subscribers or queried explicitly.
better solution
but i think you can save value in Sharedpreferences after purcahse:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,
MODE_PRIVATE).edit();
editor.putBoolean("pay", true);
editor.apply();
then check this valu every Activity on onCreat method
to show adds or don't
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
pay = prefs.getBoolean("pay", false);
if (pay) {
show();
}else dontShow();
How can i start a Fragment in the AboutAndHelpActivity instead of the Activity itself or how can i put the Fragment in startActivity()?
//if it is the first start of the app, open HelpFragment once.
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
startActivity(new Intent(MainActivity.this, AboutAndHelpActivity.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
Before you want to think about activity and fragment
I will suggest you to read about it
So that you will get idea
Please check the link for fragment information
https://developer.android.com/guide/components/fragments.html
You can add, remove or replace the fragment in the activity
You should manage fragment life cycle.
Otherwise you might get some unexpected UI or app issues
You are completely misunderstand what is Activities and Fragments. Read about them again.
tl;dr: Activity - base unit. Fragment - part. Fragment can be only hosted in some activity, and can't be displayed other way.
Here how you can display fragment in activity:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_holder,new MyFragment())
.addToBackStack(MyFragment.class.getSimpleName())
.commit();
If I understood you correctly, AboutAndHelpActivity is supposed to be started on the app's first run and house a Fragment.
If so, the starting the Activity part is already solved in you initial question, so what remains is showing the Fragment. Two options make sense to me:
In case AboutAndHelpActivity's only job is to house the Fragment and won't be started for any other purpose, the Fragment can be added to its layout xml.
Otherwise, the Fragment needs to be added dynamically based on an Intent extra as already pointed out by #Ekalips.
I'm making a button in xml, like this:
<Button
android:id="#+id/buttondp"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thisisstringtext" />
and I want it to direct it to another page coded in xml. Can anyone help me out?
Make another activity and use
setContentView(R.layout.your_other_layout);
inside of it.
Then in the onClickListener for your button put this:
Intent i = new Intent(YourActivity.this, YourOtherActivity.class);
startActivity(i);
You can add the onclick listener to your button, so: android:onclick="method_in_your_activity".
In your activity added the method (method_in_your_activity) and add startActivity(NewActivity).
If you dynamically want to change the Content of your activity you can always call setContentView(my_layout) and change the content. However; its best practice to use another Activity.
You can use different activities for different layout. But if you want to use same activity for different layout then you should go for ViewFlipper . You can also get some animation when switching from one view to another. Tutorial regarding the same can be found here.
using this code in java file you will click button to redirect next page
public void onClick(View v)
{
Intent ia=new Intent(getApplicationContext(),second.class);
startActivity(ia);
}
In your button XML, add :
android:onCLick="myRedirectFunction"
In your MyMainActivity.java, add function named myRedirectFunction and inside that function :
Intent homepage = new Intent(MyMainActivity.this, MySubActivity.class);
startActivity(homepage);
I'm very new to Android development, and want to make sure that I'm structuring my application correctly. First, let me explain what is needed.
The application starts off prompting the user for an access code, depending on their response there are two resulting menu's which can appear. One menu has 5 buttons, while the other adds two extra buttons making seven. Each one of those buttons brings me to a different view where more information will be displayed.
I originally starting writing it with one activity and a different XML file for each view. However, the more I have been researching online it seems that I should have a different Activity for each individual view. But now I'm relatively confused how I can prompt the user for input before initializing any of the Activities.
If anyone has any input I'd really appreciate it.
Thanks
You will need to initialize an activity before getting user input. And I think it is common that if you go to a new view that it uses a different class and xml layout. So for each of the new views you could make a new class that extends an activity and then has an xml file related to that view.
So have these 2 files for each new view you show.
Java file:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
//add any views
</LinearLayout>
Try:
-push activity1 with layout1
-pop inputDialog
-when inputDialog is closed by clicking ok...
-push Activity2 with layout2, proceed with you input from activity1 using extras
...and so on ;)
I have been trying to break up my programs into an activity and a corresponding xml layout for each view. If you have one activity and all those layouts, you have the potential to have a monster block of code in that one activity. I find that breaking it up makes it easier to read and debug.
As for prompting the user before initializing activities, i'm not entirely clear on what you mean. You need to load an activity before anything happens, in your situation it could easily be a simple password acception activity. If you're talking about passing information between activities, you can package data in an intent, and use that to start a new activity. Then in that new activity pull the information out of the intent.