Sedentary behaviors and physical inactivity are not only increasing worldwide but also are critical risk factors for adverse health outcomes. Yet, few studies have examined the effects of sedentary behavior on cognition or the long-term role of either behavior in early to middle adulthood. To investigate the association between 25-year patterns of television viewing and physical activity and midlife cognition. Prospective study of 3247 adults (black and white races; aged 18-30 years) enrolled in the Coronary Artery Risk Development in Young Adults (CARDIA) Study (March 25, 1985, to August 31, 2011). Data analysis was performed June 1, 2014, through April 15, 2015. We assessed television viewing and physical activity at repeated visits (≥3 assessments) over 25 years using a validated questionnaire. A 25-year pattern of high television viewing was defined as watching TV above the upper baseline quartile (>3 hours/d) for more than two-thirds of the visits, and a 25-year pattern of low physical activity was defined as activity levels below the lower, sex-specific baseline quartile for more than two-thirds of the of the visits. We evaluated cognitive function at year 25 using the Digit Symbol Substitution Test (DSST), Stroop test, and Rey Auditory Verbal Learning Test. At baseline, the mean (SD) age of the 3247 study participants was 25.1 (3.6) years, 1836 (56.5%) were female, 1771 (54.5%) were white, and 3015 (92.9%) had completed at least high school. Compared with participants with low television viewing, those with high television viewing during 25 years (353 of 3247 [10.9%]) were more likely to have poor cognitive performance (<1 SD below the race-specific mean) on the DSST and Stroop test, with findings reported as adjusted odds ratio (95% CI): DSST, 1.64 (1.21-2.23) and Stroop test, 1.56 (1.13-2.14), but not the Rey Auditory Verbal Learning Test, adjusted for age, race, sex, educational level, smoking, alcohol use, body mass index, and hypertension. Low physical activity during 25 years in 528 of 3247 participants (16.3%) was significantly associated with poor performance on the DSST, 1.47 (1.14-1.90). Compared with participants with low television viewing and high physical activity, the odds of poor performance were almost 2 times higher for adults with both high television viewing and low physical activity in 107 of 3247 (3.3%) (DSST, 1.95 [1.19-3.22], and Stroop test, 2.20 [1.36-3.56]). High television viewing and low physical activity in early adulthood were associated with worse midlife executive function and processing speed. This is one of the first studies to demonstrate that these risk behaviors may be critical targets for prevention of cognitive aging even before middle age.